简体   繁体   English

Rails + OmniAuth Facebook:如何获取Access Token?

[英]Rails + OmniAuth Facebook: how to obtain Access Token?

I am trying to fetch the list of friends from Facebook. 我正在尝试从Facebook获取朋友列表。 Sign in through Facebook is not a problem, but the problem is to fetch person's friends - because of access token . 通过Facebook登录不是问题,但问题是获取人的朋友 - 因为access token

puts request.env["omniauth.auth"].inspect
puts '==='
@user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
@fb_user = FbGraph::User.fetch(@user.uid).friends
puts @fb_user.inspect

The problem is on the #4 line - in this case I am getting error 问题出在第4行 - 在这种情况下,我收到了错误

OAuthException :: An access token is required to request this resource.

When I put there something like this: 当我把它放在这样的东西:

@fb_user = FbGraph::User.fetch(request.env["omniauth.auth"].credentials.token).friends

I'll get 我去拿

OAuthException :: (#803) Some of the aliases you requested do not exist: PRINTED OUT TOKEN

What's the proper way to obtain the access token ? 获取access token的正确方法是什么?

EDIT: Current flow 编辑:当前流量

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
    @fb_user = FbGraph::User.fetch(request.env["omniauth.auth"].credentials.token).friends
    if !@user
      flash[:error] = 'This email address is already used in the system.'
      redirect_to :back
    elsif @user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
      sign_in_and_redirect @user, :event => :authentication
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end

In User model: 用户模型中:

  def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
    data = access_token.extra.raw_info
    if user = User.where(:provider => 'facebook', :uid => data.id).first
      user
    elsif user = User.where('email = ? AND provider IS NULL', data.email).first
      return false
    else
      ...saving data...
    end
    return user if user
  end

You can get an access token for test purposes via the Facebook Graph API Explorer . 您可以通过Facebook Graph API Explorer获取用于测试目的的访问令牌。 Make sure you select the proper fields that you want access to, and click "get access token". 确保选择要访问的正确字段,然后单击“获取访问令牌”。 A more permanent solution is to register your app with Facebook so that you will be able to continually make requests without the token dying. 更永久的解决方案是在Facebook上注册您的应用程序,这样您就可以在没有令牌死亡的情况下不断提出请求。

You should look into the Facebook OAuth dialogue . 您应该查看Facebook OAuth对话

I'm assuming you're trying to use the OAuth2 strategy instead of the Javascript SDK. 我假设您尝试使用OAuth2策略而不是Javascript SDK。 Make sure you have set up a callback url like so: 确保您已设置回调网址,如下所示:

client.redirect_uri = "http://your.client.com/facebook/callback"

In the controller that handles your callback, you should do something like this: 在处理回调的控制器中,您应该执行以下操作:

client.authorization_code = params[:code] 
access_token = client.access_token! :client_auth_body 
FbGraph::User.me(access_token).fetch

Make sure you've let fb_graph know what your app's id and secret are. 确保你让fb_graph知道你的应用程序的ID和秘密是什么。 You should look into this stackoverflow to keep your apps info safe. 您应该查看此stackoverflow以确保您的应用程序信息安全。

I'll also plug the koala gem 我也会插上考拉宝石

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM