Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook OAuth: custom callback_uri parameters

I'd like to have a dynamic redirect URL for my Facebook OAuth2 integration. For example, if my redirect URL is this in my Facebook app:

http://www.mysite.com/oauth_callback?foo=bar

I'd like the redirect URL for a specific request be something like this, so that on the server, I have some context about how to process the auth code:

http://www.mysite.com/oauth_callback?foo=bar&user=6234

My redirect gets invoked after the authorization dialog is submitted, and I get back an auth code, but when I try to get my access token, I'm getting an OAuthException error back from Facebook. My request looks like this (line breaks added for clarity):

 https://graph.facebook.com/oauth/access_token     ?client_id=MY_CLIENT_ID     &redirect_uri=http%3A%2F%2Fwww.mysite.com%2Foauth_callback%3Ffoo%3Dbar%26user%3D6234     &client_secret=MY_SECRET     &code=RECEIVED_CODE 

All of my parameters are URL-encoded, and the code looks valid, so my only guess is that the problem parameter is my redirect_uri. I've tried setting redirect_uri to all of the following, to no avail:

  1. The actual URL of the request to my site
  2. The URL of the request to my site, minus the code parameter
  3. The URL specified in my Facebook application's configuration

Are custom redirect URI parameters supported? If so, am I specifying them correctly? If not, will I be forced to set a cookie, or is there some better pattern for supplying context to my web site?

like image 227
Jacob Avatar asked Jun 24 '11 03:06

Jacob


People also ask

How do I make valid OAuth redirect URIs on Facebook?

You should try following for the redirect url. Your redirect URI will typically have the format https://engage-app name.rpxnow.com/facebook/callback. For example, if your Engage app has the name my--test-app, your redirect URI would be https://my-test-app.rpxnow.com/facebook/callback.

How do I add this OAuth redirect URI to my Facebook app configuration?

In your app settings on https://developers.facebook.com/sa/apps/<my-app-id> , make sure that you add the Facebook Login product. Then under "Client OAuth Settings" enter the URL in the "Valid OAuth redirect URIs" box.

How do I turn off use strict mode for redirect URIs on Facebook?

Due to the security changes made to Facebook, it's no longer possible to turn off this setting. If you are using HTTPS, you will need to enter the URI with the port number as well i.e. This last point is not related to the recent Facebook app changes.

How does OAuth work with Facebook?

OAuth for Signing In The service checks to see who you are on Facebook and creates a new account for you. When you sign into that service in the future, it sees that you're sign in with the same Facebook account and gives you access to your account.


2 Answers

I figured out the answer; rather than adding additional parameters to the redirect URL, you can add a state parameter to the request to https://www.facebook.com/dialog/oauth:

 https://www.facebook.com/dialog/oauth     ?client_id=MY_CLIENT_ID     &scope=MY_SCOPE     &redirect_uri=http%3A%2F%2Fwww.mysite.com%2Foauth_callback%3Ffoo%3Dbar     &state=6234 

That state parameter is then passed to the callback URL.

like image 58
Jacob Avatar answered Oct 10 '22 22:10

Jacob


If, for any reason, you can't use the option that Jacob suggested as it's my case, you can urlencode your redirect_uri parameter before passing it and it will work, even with a complete querystring like foo=bar&morefoo=morebar in it.

like image 34
Manuel Pedrera Avatar answered Oct 10 '22 21:10

Manuel Pedrera