2017-03-20 33 views
0

我已经创建了一个应用程序与使用Facebook登录:Ruby on Rails的,Facebook登录得到朋友和的accessToken

gem "koala" 
gem 'omniauth' 
gem 'omniauth-facebook', '1.4.0' 
gem 'fb_graph2' 

我想给谁使用的应用程序也用户朋友的访问。 但我不知道如何做到这一点,以及如何获得'访问令牌',因为我不明白什么是访问令牌...

现在,这里是我的代码。 型号/ user.rb

class User 
    include Mongoid::Document 
    include Mongoid::Timestamps 

    field :provider, type: String 
    field :uid, type: String 
    field :name, type: String 
    field :picture, type: String 
    field :auth_token, type: String 

    has_many :parties 
    has_many :invitations, :class_name => 'Invite', :foreign_key => 'recipient_id' 
    has_many :sent_invites, :class_name => 'Invite', :foreign_key => 'sender_id' 

    has_many :friends 

    # TODO: Ajouter les amitiés 
    # TODO: Ajouter les recherches d'amis (livereload) 

    def self.create_with_omniauth(auth) 
    create! do |user| 
     user.provider = auth['provider'] 
     user.uid = auth['uid'] 
     user.auth_token = auth['credentials']['token'] 
     if auth['info'] 
     user.name = auth['info']['name'] || "" 
     user.picture = auth['info']['image'] || "" 
     end 
    end 
    end 

    def large_image 
    return "http://graph.facebook.com/#{self.uid}/picture?type=large" 
    end 

    def normal_image 
    return "http://graph.facebook.com/#{self.uid}/picture?type=normal" 
    end 


end 

控制器/ sessions_controller.rb

class SessionsController < ApplicationController 
    def create 
    auth = request.env["omniauth.auth"] 
    user = User.where(:provider => auth['provider'], 
         :uid => auth['uid']).first || User.create_with_omniauth(auth) 
    session[:user_id] = user.id 
    redirect_to root_url, :notice => "Signed in!" 
    end 

    def destroy 
    reset_session 
    redirect_to root_url, :notice => 'Signed out!' 
    end 

    def new 
    redirect_to '/auth/facebook' 
    end 

    def failure 
    redirect_to root_url, :alert => "Authentication error: #{params[:message].humanize}" 
    end 
end 

初始化/ omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do 
    provider :facebook, '264721750639419', '94ad1cba6135ff513146d38e16e99efa' 
end 

和我的朋友控制器:

class FriendsController < ApplicationController 
    def index 
    if params[:code] 
     session[:access_token] = session[:oauth].get_access_token(params[:code]) 
    end 
    # auth established, now do a graph call: 
    @api = Koala::Facebook::API.new(session[:access_token]) 
    @user_profile = @api.get_object("me") 
    @friends = @api.get_connections(@user_profile['id'], "friends") 

    end 
end 

它不工作,我有这样的错误:

type: OAuthException, code: 2500, message: An active access token must be used to query information about the current user., x-fb-trace-id: BeOG6OGemO9 [HTTP 400] 

我想,那是因为我的代码是不正确的,所以我希望有人要帮助我提高我的代码,并获得用户的朋友的访问!

回答

0

您在users.auth_token字段中保存的令牌是您所需要的。因此,当初始化一个新的Koala实例时,通过用户auth_token - @api = Koala::Facebook::API.new(current_user.auth_token),然后尝试获取好友列表,就像您当前所做的那样。

关于什么的访问令牌是你可以在这里读到它(Facebook的上下文中)https://developers.facebook.com/docs/facebook-login/access-tokens

+0

感谢大家的回答哥们!其实,我没有这个错误,但我没有我的朋友列表,而我的数据库中有1个朋友。我有我的帐户,我有一个我在facebook上创建的其他帐户来测试..并且他没有出现在朋友页面上,你知道为什么吗? –

+0

你能更具体吗?很难从你的描述中明白什么不起作用 – Kkulikovskis

+0

是的!我有2个脸书帐户,都是朋友,都注册在我的应用程序,但是,当我要去的朋友页面,我想列出所有使用该应用程序的朋友,没有名字,但它不是正常,因为我应该看到我的2脸书帐户出现,因为我们是脸书上的朋友,我们都使用该应用程序..这更清楚吗? :) –