# Ruby's oauth2 grant\_type

**URL:** <https://www.ruby-forum.com/t/rubys-oauth2-grant-type/195130>\
**Category:** Rails\
**Created:** [September 17, 2010, 12:41pm UTC](https://www.ruby-forum.com/t/rubys-oauth2-grant-type/195130 "2010-09-17T12:41:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![axyd80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@axyd80](https://www.ruby-forum.com/u/axyd80)\
**Post date:** [September 17, 2010, 12:41pm UTC](https://www.ruby-forum.com/t/rubys-oauth2-grant-type/195130/1 "2010-09-17T12:41:17Z")

</div>

Hi, i started using oauth2 gem by intridea  
([GitHub - oauth-xx/oauth2: A Ruby wrapper for the OAuth 2.0 protocol.](http://github.com/intridea/oauth2)) and don’t know how to fix this  
problem. I have developed both client and server and on request for  
access\_token i see no grant\_type parameter. My code from client callback  
controller

class CallbackController \< Devise::OauthCallbacksController  
def accounts  
access\_token = accounts\_config.access\_token\_by\_code(params[:code])  
@user = User.find\_for\_accounts\_oauth(access\_token,  
signed\_in\_resource)

```
if @user.persisted? && @user.errors.empty?
  sign_in @user
  set_oauth_flash_message :notice, :success
  redirect_to after_oauth_success_path_for(@user) #=> redirects to

```

user\_root\_path or root\_path  
else  
session[:user\_accounts\_oauth\_token] = access\_token.token  
render\_for\_auth #=\> renders sign up view by default  
end

end  
end  
and from User model

class User \< ActiveRecord::Base  
devise :database\_authenticatable, :oauthable

def self.find\_for\_accounts\_oauth(access\_token,  
signed\_in\_resource=nil)

```
data =

```

ActiveSupport::JSON.decode(access\_token.get(Settings.oauth.site +  
Settings.oauth.access\_token\_path))

```
if user = User.find_by_username(data["username"])
  user
else
  # Create an user with a stub password.
  User.create!(:username => data["username"], :password =>

```

Devise.friendly\_token)  
end

end  
end  
Logs from provider

Started POST “/oauth/token” for 127.0.0.1 at 2010-09-17 00:17:44 +0400  
Processing by Oauth::TokenController#get\_token as _/_  
Parameters:  
{“client\_id”=\>“9ddf5f526127a8858485f2c9401c7152cfaf870da62267e6f54643de53eb6a76”,  
“client\_secret”=\>“84388e2ca839c2834177024a6f358b1415bcd3ea936be1148443d9df2f7cf363”,  
“redirect\_uri”=\>“[http://test.local.lo/users/oauth/accounts/callback](http://test.local.lo/users/oauth/accounts/callback)”,  
“type”=\>“web\_server”,  
“code”=\>“d264c2496d0dc5c494b7269f2f9e4c30cd55a571b6944d3231f63577acd12b1b”}  
SQL (0.8ms) SELECT a.attname, format\_type(a.atttypid,  
a.atttypmod), d.adsrc, a.attnotnull  
FROM pg\_attribute a LEFT JOIN pg\_attrdef d  
ON a.attrelid = d.adrelid AND a.attnum = d.adnum  
WHERE a.attrelid = ‘“oauth\_clients”’::regclass  
AND a.attnum \> 0 AND NOT a.attisdropped  
ORDER BY a.attnum  
Oauth error: invalid grant\_type  
Rendered text template (0.0ms)  
Completed 400 Bad Request in 112ms (Views: 37.9ms | ActiveRecord:  
178.2ms)  
and from client

Started GET “/users/oauth/accounts/callback?  
code=d264c2496d0dc5c494b7269f2f9e4c30cd55a571b6944d3231f63577acd12b1b&&expires\_in=3599”  
for 127.0.0.1 at 2010-09-17 00:17:44 +0400  
Processing by Devise::OauthCallbacksController#accounts as HTML  
Parameters:  
{“code”=\>“d264c2496d0dc5c494b7269f2f9e4c30cd55a571b6944d3231f63577acd12b1b”,  
“expires\_in”=\>“3599”}  
Completed in 343ms

OAuth2::HTTPError (Received HTTP 400 during request.):

Rendered /opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-3.0.0/lib/  
action\_dispatch/middleware/templates/rescues/\_trace.erb (1.4ms)  
Rendered /opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-3.0.0/lib/  
action\_dispatch/middleware/templates/rescues/\_request\_and\_response.erb  
(30.0ms)  
Rendered /opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-3.0.0/lib/  
action\_dispatch/middleware/templates/rescues/diagnostics.erb within  
rescues/layout (36.7ms)  
Any idea how to fix it?
