I have a rails project using https://github.com/intridea/oauth2 . In my rails application, I have the following code:
ApplicationController.rb
def facebook_client OAuth2::Client.new(FacebookOauthCredentials::APP_ID, FacebookOauthCredentials::APP_SECRET, :site => 'https://graph.facebook.com') end
FacebookController.rb
def facebook_session_create(poster, featured_item) redirect_to facebook_client.web_server.authorize_url(:scope => 'publish_stream', :redirect_uri => "http://localhost:3000/facebook/facebook_callback") end def facebook_callback if(params[:code]) begin access_token = facebook_client.web_server.get_access_token(params[:code], :redirect_uri => "http://localhost:3000/facebook/facebook_callback") access_token.post('/me/feed', "testing #{rand(1000)}") rescue OAuth2::HTTPError => e render :text => e.response.body end end end
Every time I run this code, I get this answer:
{"error":{"type":"OAuthException","message":"Error validating verification code."}}
However, I use the sinatra application provided in the readme gem OAuth2 file, it works fine.
def client OAuth2::Client.new(FacebookOauthCredentials::APP_ID, FacebookOauthCredentials::APP_SECRET, :site => 'https://graph.facebook.com') end get '/auth/facebook' do redirect client.web_server.authorize_url( :redirect_uri => redirect_uri, :scope => 'publish_stream' ) end get '/auth/facebook/callback' do access_token = client.web_server.get_access_token(params[:code], :redirect_uri => redirect_uri) begin user = JSON.parse(access_token.post('/me/feed', :message => "testing # {rand(10000)}")) rescue Exception => e return e.response.body end user.inspect end def redirect_uri uri = URI.parse(request.url) uri.path = '/auth/facebook/callback' uri.query = nil uri.to_s end
I tried to reproduce the steps using irb, but I have an http 400 error. I am not sure if this is for the same reason as the rails application, or if it is due to the fact that I am running a hybrid of console and web browser work. Any suggestions would be greatly appreciated.
source share