【问题标题】:What is the Best practice for passing access_token to make an API call传递 access_token 进行 API 调用的最佳实践是什么
【发布时间】:2013-01-07 20:34:26
【问题描述】:
我开发了一个使用 Oauth 2 进行授权的 Rails 应用程序。
目前为了访问 API 调用,我将 access_token 作为 URL 参数传递,我听说令牌也可以作为 Header 传递。
传递 access_token 的最佳做法是什么?为什么?
我正在使用 Devise + Doorkeeper 来支持 Oauth。
请给我一些建议..
【问题讨论】:
标签:
ruby-on-rails
ruby-on-rails-3
oauth
oauth-2.0
【解决方案1】:
作为标题,您可以在控制器中这样制作:
before_filter :authenticate
protected
def current_user
@current_user
end
def authenticate
token = request.env["HTTP_ACCESS_TOKEN"]
@current_user = User.where(authentication_token: token).first if token.present?
unless token && current_user.present?
#error handling goes here.
end
end
【解决方案2】:
在标头中看到 access_token 是很常见的。
看:标题有什么用?它们用于有关请求的元信息。毫无疑问,accesstoken 是元信息。
我不确定我的答案是否完整,可能还有一些我不知道的额外陷阱。如果我错了,请纠正我。