Reputation: 5521
Authentication via Facebook does result in the following error message:
Koala::Facebook::AuthenticationError in AuthenticationsController#menu
type: OAuthException, code: 190, message: Malformed access token AQBiaE1v-pbSitzgNirHKSm7zNp3XoLAeHsvlFB626lARPBQCN98SBgIczjoRj3h8RQSVunm8gu-fHbO3H8-_9Ef9a5Lt00ixQ-wgum9p9FM5xN3WUvgc2BSyy1it2G4XlHNbQuwKYvsN-_7juH2NSXxMZmpaXh4qjjm13HWIjkYBWuyTIuJTJ7yUc97XixSMJtDbIIEBXfK52m_zIBTKvA4m8IoTOHDDoloeIhmARrGlMCmQG_vWZSMc.. (deleted the last characters by author) [HTTP 400]
Basically, the login seems to work but when I try to access my profile via @graph.get_object("me")
the token is not accepted.
The console output for the 2nd working example seems to be ok.
I may get the wrong token, but why? It works with a fixed access_token generated on http://developers.facebook.com/tools/explorer
My Rails controller code:
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
Another example using Sinatra which is working:
#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
Upvotes: 0
Views: 1138
Reputation: 5521
I could resolve the issue by using the omniauth
and the omniauth-facebook
gem.
Controller:
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
redirect_to root_path
end
protected
def auth_hash
request.env['omniauth.auth']
end
end
Initializer (Omniauth.rb)
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, '1234', '1233',
:scope => 'read_stream'
end
Routes:
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
Upvotes: 1