Facebook SDK iOS连接的Rails-api身份验证?

Sam*_*ang 5 ruby api ruby-on-rails ios ruby-on-rails-4

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

  1. 用户打开移动应用程序
  2. 用户点击Facebook连接
  3. 移动应用获取fb_access_token并将其发布到API服务器以识别用户
  4. API服务器使用fb_access_token在Facebook上获取用户个人资料
  5. API服务器创建并查找用户,然后使用api_token响应此特定用户
  6. 移动应用程序之后使用api_token响应进行所有通信.

哪个身份验证应该是此应用的最佳选择?oAuth2还是BasicAuth?我尝试过带门卫的rails-api,但由于门卫需要一些资产,因此无法开箱即用.

Mar*_*čič 4

我正在为此集成设备进行基本身份验证。

首先,我从移动应用程序获取发布参数(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=

  • 我对此进行了一些定制,但我将 user_id 和 secure_code (我自己生成)发送回移动设备...应用程序内的所有其他通信都是通过 user_id 连接并使用安全代码进行验证...我仅当用户使用 Facebook 登录时才使用 access_token。 (2认同)