Problems with OAuth 2 Gem

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.

+4
source share
2 answers

I found the answer to my problem on this page. Facebook Graphic API - OAuth Token

I ran into the same problem, but it turned out that the problem was not in the encoding redirect_uri parameter, or that I had a trailing slash or question mark just that I went through two different redirect URLs (did not read at that time).

Redirect_uri is used only as a redirect once (first time) to redirect back to the relying side with the marker "code". The second time, redirect_uri is passed back to auth, but this time it is not used as you expected (for redirection), rather it is used by authentication to verify the code. The server responds with access_token.

You will see the documentation on facebook (which is awful) says the choice "Exchange it for access token access ...."

So I didn’t need to code or do anything special for Uri, just go through the same redirect_uri twice, and select the second page to get the access_token inside.

I did not correctly copy my source code and redirected the uri that I passed in order to get a code different from the one I missed in order to get the access token. The API documentation for Facebook is terrible :(

+2
source

I had this problem / error, but in the end I found another solution (comparing the settings of my Dev application) that were in my prod application (which created this error).

I went to the advanced settings page for my application in the application for Facebook developers: https://developers.facebook.com/apps/YourAppID/advanced

Then I set the parameter "Encrypted Access Token:" and turned it on to "Enabled".

0
source

Source: https://habr.com/ru/post/1337281/


All Articles