Sam*_*ang 5 ruby api ruby-on-rails ios ruby-on-rails-4
我打算使用rails-api为iOS移动应用程序提供JSON API.过程:
哪个身份验证应该是此应用的最佳选择?oAuth2还是BasicAuth?我尝试过带门卫的rails-api,但由于门卫需要一些资产,因此无法开箱即用.
我正在为此集成设备进行基本身份验证。
首先,我从移动应用程序获取发布参数(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
Run Code Online (Sandbox Code Playgroud)
现在 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
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助
您也可以为 google 使用相同的代码,只需将 url 更改为“ https://www.googleapis.com/oauth2/v2/userinfo?access_token= ”