我打算使用rails-api为iOS移动应用程序提供JSON API以供使用。流程:

  • 用户打开移动应用程序
  • 用户在Facebook上点按connect
  • 移动应用获取fb_access_token并将其发布到API服务器以识别用户
  • API服务器使用fb_access_token
  • 在Facebook上获取用户个人资料
  • API服务器要么创建并查找用户,然后为此特定用户使用api_token响应
  • 移动应用程序随后将api_token响应用于所有通信。

  • 哪种身份验证应该是此应用的最佳选择? 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=

    10-01 18:10
    查看更多