Facebook OAuth: custom callback_uri parameters
I was trying to implement a Facebook login workflow against API v2.9 following this tutorial. I tried the solutions described above. Manuel's answer is sort of correct, but what I observed is url encoding is not needed. Plus, you can only pass one parameter. Only the first query parameter will be considered, the rest will be ignored. Here is an example,
Request a code via
https://www.facebook.com/v2.9/dialog/oauth?client_id={app-id}&redirect_uri=http://{url}/login-redirect?myExtraParameter={some-value}
You'd get a callback for your url. It will look like
http://{url}/login-redirect?code={code-from-facebook}&myExtraParameter={value-passed-in-step-1}
. Note that facebook would make a callback withmyExtraParameter
. You can extract the value formyExtraParameter
from callback url.Then you can request access token with
https://graph.facebook.com/v2.9/oauth/access_token?client_id={app-id}&client_secret={app-secret}&code={code-from-facebook}&redirect_uri=http://{url}/login-redirect?myExtraParameter={value-extracted-in-step-2}
Additional parameter passed in step 1 after the first query parameter will be ignored. Also make sure to not include any invalid characters in your query parameter (see this for more information).
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.
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.