How to get authorization code from Facebook javascript SDK

I have a / omniauth development method, and now I would like to use the javascript SDK to log in / ask for permission, and then redirect them to my omniauth callback controller.

This is what I have (coffeescript).

$('#fb-connect').live 'click', -> FB.login ((response) -> if response.authResponse window.location = "/users/auth/facebook/callback?code=" + response.authResponse.signedRequest else console.log "User cancelled login or did not fully authorize." ), scope: "email, offline_access" false 

But I get an Invalid verification code format error. I guess this is because param code expects something other than a signed request?

Refresh

So it looks like I need to pass an authorization code, but I can not find a way. An example of a direct URL shows that you can specify response_type = code to get the authorization code, but I don’t know how to do it using FB.api, Any ideas?

 http://www.facebook.com/dialog/oauth/? scope=email,user_birthday& client_id=123050457758183& redirect_uri=http://www.example.com/response& response_type=code 
+2
source share
2 answers

Just run someone who stumbles on this, you don’t need to pass any parameters to your controller when using Devise / omniauth ... the following works fine

 $('#fb-connect').live 'click', -> FB.login ((response) -> if response.authResponse window.location = "/users/auth/facebook/callback else console.log "User cancelled login or did not fully authorize." ), scope: "email, offline_access" false 

EDIT

If you are using a smaller version or bypassing the exception that occurred when both parameters and code are missing, the following built-in js work with onclick.

 <script> function fb_authorise(){ FB.login(function(response) { if(response.authResponse) { window.location = "/users/auth/facebook/callback?signed_request=<%= params[:signed_request]%>" } }, {scope: "email, offline_access"}); }; </script> 

So, in response to your original question, you used the signed_request parameter, and then add the URL with the code = not signed_request

+2
source

On the client side of auth, you do not need to use "code". In this .authResponse answer you will already have access_token access. FB.login () has already done all this for you. If you have "window.location = ...", the user is logged in.

Check the authentication document - make sure you read the client section. Also check out the fb.login document

0
source

All Articles