Showing posts with label Facebook. Show all posts
Showing posts with label Facebook. Show all posts

Thursday, October 27, 2011

Facebook Graph API and redirect_uri and QueryString parameters

PROBLEM: If you are using the Facebook API e.g. to do the login for your website etc (http://developers.facebook.com/docs/authentication/), you may find yourself wanting to get the user to login to Facebook but then return to the page they came from. My first reaction was to add the URL they came from to the end of the redirect_uri querystring parameter e.g.

https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL?orig_url=http://example.com/default.aspx

Of course you would url encode the orig_url parameter and then decode it when you wanted to use it... it actually does work for the above type of call... but when calling the app authentication api it doesn't like it. Base64 encoding it doesn't work either. And remember, a little known fact is that the redirect_uri needs to be the same for when you are doing the app authorization as the app authentication (see below url)

https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&redirect_uri
=YOUR_URL?orig_url=http://example.com/default.aspx&client_secret=YOUR_APP_SECRET&code=THE_CODE_FROM_ABOVE

Bottom line is that I tried the above approaches and kept getting a HTTP 400 error "Error validating verification code.".

SOLUTION: Taking a step back, rather than try and get what should work working, try a different approach. In my case, simply put the original URL into a session variable for use later on (http://stackoverflow.com/questions/5747320/how-to-encode-the-redirect-uri-for-facebook-login).

e.g.

Session["UrlToReturnToAfterLogin"] = HttpContext.Current.Request.Url.OriginalString;

Too easy.