我计划使用rails-api为iOS移动应用程序消费提供JSON。这一进程:
哪个认证应该是这个应用程序的最佳选择?oAuth2还是BasicAuth?我和门卫一起尝试了rails-api,但是由于看门人需要一些资产,所以它无法发挥作用。
发布于 2014-01-13 09:18:14
我正在对此进行基本的验证,并将其与设计集成在一起。
首先,我从移动应用程序( access_token和其他东西)获得post参数。
然后,我使用open从facebook获取用户详细信息:
url = "https://graph.facebook.com/me?access_token="
begin
content = open(URI.encode(url + params[:user][:access_token]))
rescue OpenURI::HTTPError #with this I handle if the access token is not ok
return render :json => {:error => "not_good_access_token" }
end现在Facebook返回响应
status = content.status[0]
content = ActiveSupport::JSON.decode(content)
if status == "200"
#get the email and check if the user is already in the database. If there is not email, check by the facebook id
#If the user exists return the user. If the user does not exists create new希望这能有所帮助
也可以为google使用相同的代码,只需将url改为"token=“即可。
发布于 2014-01-13 09:24:32
我会尝试一下无所不在-facebook,因为它使用OAuth2,而且非常容易使用。所有无所不在的策略都是rails中间件,因此您只需将gem 'omniauth-facebook'添加到您的key文件中,并将以下内容添加到config/initializers/omniauth.rb中,您就可以使用/auth/facebook通过facebook和/auth/facebook/callback登录来创建用户会话(您可能希望更改:display value,因为移动中的弹出可能不美观):
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'],
:scope => 'email,user_birthday,read_stream', :display => 'popup'
endfacebook令牌将位于返回到回调端点的request.env['omniauth.auth'][:credentials][:token]中,您可以将其集成到移动身份验证策略中。有关详细信息,请参阅上面的链接和总括主页。
https://stackoverflow.com/questions/21086375
复制相似问题