我正在尝试通过Facebook验证并收到以下错误消息
在AuthenticationsController#menu中的Koala :: Facebook :: AuthenticationError 类型:OAuthException,代码:190,消息:格式错误的接入令牌AQBiaE1v-pbSitzgNirHKSm7zNp3XoLAeHsvlFB626lARPBQCN98SBgIczjoRj3h8RQSVunm8gu-fHbO3H8-_9Ef9a5Lt00ixQ-wgum9p9FM5xN3WUvgc2BSyy1it2G4XlHNbQuwKYvsN-_7juH2NSXxMZmpaXh4qjjm13HWIjkYBWuyTIuJTJ7yUc97XixSMJtDbIIEBXfK52m_zIBTKvA4m8IoTOHDDoloeIhmARrGlMCmQG_vWZSMc ..(由作者删除最后字符)[HTTP 400]
这是我的rails控制器的代码,显示错误。基本上登录似乎工作。但是当我尝试通过@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 :(得分:1)
正如我在评论中指出的那样,我遇到了返回令牌的问题,但是我无法在Rails中使用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
我知道我还没有将会话/用户/应用程序控制器分开(但是)但是我想与你分享我的解决方案。欢迎任何评论。