如何返回Doorkeeper访问令牌作为json [英] How to return Doorkeeper access token as json

查看:374
本文介绍了如何返回Doorkeeper访问令牌作为json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为iOS应用创建一个登录系统,后台由设计师和门户管理。



我想限制网络请求,所以不想从凭据获取令牌,然后将用户的详细信息作为单独的请求。



这是我目前的尝试:

  token = Doorkeeper :: AccessToken.create!(application_id:@application_id,
resource_owner_id:current_user.id,:expires_in => 168.hours)
puts token.token
render:json => {:user => current_user,:token => token.as_json(:include => token)},
status::ok,location::users

然而,返回的是:

  {user:{id:2,email :user3@test.com,created_at:2014-06-12T17:25:12.000Z,
updated_at:2014-06-13T12:20:18.536Z,
firstName:user,lastName:test,subscription:null},
token:{resource_owner_id:2,scopes:[],expires_in_seconds :604800,
application:{uid:[Filtered]}}}

所以实际的access_token密钥没有被传回,以允许我进行未来的呼叫。
我可以看到令牌本身没有在 DoorKeeper :: AccessToken.as_json 中返回,但是 token.as_json(:include =有没有人知道如何将AccessToken,包括访问令牌本身返回给json? ?

解决方案

我设法解决这个问题的方法是创建自己的AccessToken类,重载as_json方法以包含字段I通缉。



例如

  class AccessToken< Doorkeeper :: AccessToken 
def as_json(options = {})
{
:token => self.token,
#:resource_owner_id => self.resource_owner_id,
#:scopes => self.scopes,
:created_at => self.created_at,
:expires_in_seconds => self.expires_in_seconds,
#:application => {:uid => self.application.uid}
}
end
end

如果有人有更好的解决方案,我都耳朵


I am trying to create a log in system for an iOS app with a rails back end powered by devise and door keeper.

I want to limit the number of network requests so don't want to have to get the token from credentials then get the user details as a separate request.

Here is my current attempt:

token = Doorkeeper::AccessToken.create!(application_id: @application_id,
    resource_owner_id: current_user.id, :expires_in => 168.hours)
puts token.token
render :json => {:user => current_user, :token => token.as_json(:include=> token)}, 
    status: :ok, location: :users

However what is being returned is:

{"user":{"id":2,"email":"user3@test.com","created_at":"2014-06-12T17:25:12.000Z",
"updated_at":"2014-06-13T12:20:18.536Z",
"firstName":"user","lastName":"test","subscription":null},
"token":{"resource_owner_id":2,"scopes":[],"expires_in_seconds":604800,
"application":{"uid":"[Filtered]"}}}

So the actual access_token key isn't being passed back to allow me to make future calls. I can see that the token itself isn't returned in DoorKeeper::AccessToken.as_json, but token.as_json(:include=> token) still doesn't return it.

Does anyone know how to return the AccessToken, including the access token itself, as json?

解决方案

The way I managed to solve this was to create my own AccessToken class that overloads the as_json method to include the fields I wanted.

e.g

class AccessToken < Doorkeeper::AccessToken
   def as_json(options={})
      {
        :token => self.token,
        #:resource_owner_id => self.resource_owner_id,
        #:scopes => self.scopes,
        :created_at => self.created_at,
        :expires_in_seconds => self.expires_in_seconds,
        #:application => { :uid => self.application.uid }
      }
   end
end

If anyone has a better solution I'm all ears

这篇关于如何返回Doorkeeper访问令牌作为json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆