Facebook Access Token from Website cookie using new OAuth and C#
Alright, so I've seen this question asked but yet have found a simple answer that works.
A user comes to my website and has already "authorized" my website/app using the standard facebook login dialog - USING oauth.
Upon returning to my site the user has a cookie on his machine that starts with "fbsr_%" - from what I'm reading this is considered "the code".
What I'd like to do now is grab this cookie server-side, make a call to facebook, from what I found I'm supposed to call: "https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}"
then receive back the accesstoken...
I would like to receive the access token without having to redirect the user on the client. So what I have so far is as follows, and from what I've gathered it is supposed to work, but for me it always gives me a an error "The remote server returned an error: (400) Bad Request."
string url = "https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}";
string redirectUri = "http://www.mysite.com/";
string code = Request.Cookies["fbsr_" + clientId].Value;
WebRequest request = WebRequest.Create(string.Format(url, clientId, redirectUri, clientSecret, code));
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader streamReader = new StreamReader(stream, encode);
string accessToken = streamReader.ReadToEnd().Replace("access_token=", "");
streamReader.Close();
response.Close();
Any help 开发者_Go百科would be greatly appreciated
Now that Facebook require SSL for applications your "redirect_uri" need to be https (i.e https://apps.facebook.com/myapp/), otherwhise you will get "Bad Request".
The "redirect_uri" for the access_token (https://graph.facebook.com/oauth/access_token) needs to be the same as that when requesting the code that requires the user to login and connect to your app (https://graph.facebook.com/oauth/authorize or https://www.facebook.com/dialog/oauth) or you will get the same error, "Bad Request".
check this tutorial: http://amirrajan.net/Blog/asp-mvc-and-facebook-single-sign-on
精彩评论