2017-03-20 15 views
0

を取得し、私は使用してFacebookのログインでアプリを作成しました。 アクセストークンが何であるかわからないため、アクセストークンをどうやって作成するのか分かりません...ルビー、Facebookのログインが友人とAccessToken

ここには、私が持っているコードがあります。 モデル/ 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

回答仲間をありがとう!実際、私はもうエラーはありませんが、DB mongoに1人の友人がいる間に私の友達リストはありません。私は自分のアカウントを持っていて、Facebookに作成したテスト用の他のアカウントを持っています。彼は友達のページには表示されません。理由は分かりますか? –

+0

もっと具体的になりますか?あなたの記述から正確には動作しないものを理解するのは難しい – Kkulikovskis

+0

はい!私は2つのFacebookアカウントを持っていて、どちらも友人であり、どちらも私のアプリに登録されていますが、友だちのページに行くときに、そのアプリを使っているすべての友達をリストしたいところに名前はありません。私は2つのFacebookのアカウントが表示されるはずですので、私はFacebookの友人であり、我々は両方のアプリを使用しているので、これは明らかですか? :) –

関連する問題