我打算使用rails-api为iOS移动应用程序提供JSON API以供使用。流程:
哪种身份验证应该是此应用的最佳选择? oAuth2或BasicAuth?我尝试了与门卫一起使用rails-api,但由于门卫需要一些 Assets ,因此无法立即使用。
最佳答案
我正在为此设计集成一个基本的身份验证。
首先,我从移动应用程序中获取post参数(access_token和其他内容)。
然后,我使用open-api从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使用相同的代码,只需将网址更改为“https://www.googleapis.com/oauth2/v2/userinfo?access_token=”