2014-10-10 35 views
0

我試圖通過Facebook進行身份驗證,並得到以下錯誤消息鐵軌(考拉:: Facebook的:: AuthenticationError) - 試圖訪問配置文件OAuthException,代碼:190,消息:格式不正確的訪問令牌

考拉:: Facebook的:: AuthenticationError在AuthenticationsController#菜單 類型:OAuthException,代碼:190,消息:格式錯誤的接入令牌AQBiaE1v-pbSitzgNirHKSm7zNp3XoLAeHsvlFB626lARPBQCN98SBgIczjoRj3h8RQSVunm8gu-fHbO3H8-_9Ef9a5Lt00ixQ-wgum9p9FM5xN3WUvgc2BSyy1it2G4XlHNbQuwKYvsN-_7juH2NSXxMZmpaXh4qjjm13HWIjkYBWuyTIuJTJ7yUc97XixSMJtDbIIEBXfK52m_zIBTKvA4m8IoTOHDDoloeIhmARrGlMCmQG_vWZSMc ..(由作者刪除最後字符)[HTTP 400]

這是我的軌道控制器顯示錯誤的代碼。基本上登錄似乎工作。但是當我嘗試通過@graph.get_object("me")訪問我的配置文件時,令牌不被接受。

class AuthenticationsController < ApplicationController 

    APP_ID="1234" 
    APP_SECRET="1234" 
    APP_CODE="XXXX" 
    SITE_URL="http://local.myapp.com:3000/" 

    def index 
    if session['access_token'] 
     @face='Logged in -> <a href="authentications/logout">Logout</a>' 
     @graph = Koala::Facebook::API.new(session["access_token"]) 
    else 
     @face='<a href="authentications/login">Login</a>' 
    end 
    end 

    def login 
     session['oauth'] = Koala::Facebook::OAuth.new(APP_ID, APP_SECRET, SITE_URL + 'authentications/callback') 
     redirect_to session['oauth'].url_for_oauth_code() 
    end 

    def logout 
    session['oauth'] = nil 
    session['access_token'] = nil 
    redirect_to '/' 
    end 

    def callback 
     session['access_token'] = params["code"] 
     redirect_to '/authentications/menu' 
    end 

    def menu 
    @ok="Hi!" 
    if session['access_token'] 
     @face='Logged in -> <a href="/authentications/logout">Logout</a>' 
     @graph = Koala::Facebook::GraphAPI.new(session["access_token"]) 

     ## LEADS TO ERROR 
     @graph.get_object("me") 

    else 
     @face='<a href="/authentications/login">Login</a>' 
    end 

    end 
end 

而這個代碼使用sinatra的另一個例子正在工作。

#let Bundler handle all requires 
require 'bundler' 
require 'uri' 

Bundler.require(:default) 

# register your app at facebook to get those infos 
APP_ID  = 1234 
APP_SECRET = '1234' 

class SimpleRubyFacebookExample < Sinatra::Application 

    use Rack::Session::Cookie, secret: 'this_adfkaTGDGHDHJJJksk_0898932311_secret' 

    get '/' do 
    if session['access_token'] 
     'You are logged in! <a href="/logout">Logout</a>' 

     @graph = Koala::Facebook::API.new(session["access_token"]) 
     user = @graph.get_object("me") 
     feed = @graph.get_connections("me", "feed", {:limit => 999}) 
    else 
     'Logout successful <br> Click to login <a href="/login">Login</a>' 
    end 
    end 

    get '/login' do 
    session['oauth'] = Koala::Facebook::OAuth.new(APP_ID, APP_SECRET, "#{request.base_url}/callback") 
    redirect session['oauth'].url_for_oauth_code(scope: "read_mailbox,read_stream") 
    end 

    get '/logout' do 
    session['oauth'] = nil 
    session['access_token'] = nil 
    redirect '/' 
    end 

    get '/callback' do 
    session['access_token'] = session['oauth'].get_access_token(params[:code]) 
    redirect '/' 
    end 
end 

我的控制器/我的令牌有什麼問題?我輸出到控制檯,它似乎是好的。任何有用的提示表示讚賞!

+0

當我使用https://developers.facebook.com/tools/explorer/上生成的修復access_token時,它正在工作,所以我想我會以某種方式得到一個錯誤的標記。 – Christian 2014-10-10 09:12:13

回答

1

正如在我的評論中指出的那樣,我返回的令牌存在問題,但我無法使用Raala中的Koala獲取正確的令牌。

我已通過使用omniauth和omniauth-facebook gem解決了該問題。

控制器:

class AuthenticationsController < ApplicationController 

    def create 
    auth = request.env["omniauth.auth"] 
    session['fb_auth'] = auth 
    session['fb_access_token'] = auth['credentials']['token'] 
    session['fb_error'] = nil 
    redirect_to authentications_menu_path 
    end 

    def menu 
    if session["fb_access_token"].present? 
     graph = Koala::Facebook::GraphAPI.new(session["fb_access_token"]) # Note that i'm using session here 
     @profile_image = graph.get_picture("me") 
     @fbprofile = graph.get_object("me") 
     @friends = graph.get_connections("me", "friends") 
    end 
    end 

    def logout 
    session['fb_access_token'] = nil 
    session['fb_access_token'] = nil 
    redirect_to root_path 
    end 

    protected 

    def auth_hash 
    request.env['omniauth.auth'] 
    end 
end 

初始化程序(Omniauth.rb)

Rails.application.config.middleware.use OmniAuth::Builder do 
    provider :facebook, '1234', '1233', 
      :scope => 'read_stream' 
end 

途徑:

Rails.application.routes.draw do 

    match 'authentications/index', via: [:get, :post] 
    match 'authentications/login', via: [:get, :post] 
    match 'authentications/logout', via: [:get, :post] 
    match 'authentications/menu', via: [:get, :post] 
    get '/auth/:provider/callback', to: 'authentications#create' 
    root :to => "authentications#index" 

end 

我知道我還沒有分離出的會話/用戶/應用控制器(然而),但我想與你分享我的解決方案。歡迎任何評論。