开发者

Failing to retrieve access token in .NET desktop app

开发者 https://www.devze.com 2023-03-08 23:24 出处:网络
I\'m writing a .NET app that runs on a Windows computer. It is not accessible through the browser. The problem is, I can\'t authenticate like I should. I\'m currently coding in C# .NET, more specific

I'm writing a .NET app that runs on a Windows computer. It is not accessible through the browser. The problem is, I can't authenticate like I should. I'm currently coding in C# .NET, more specific in C#.

  • I have a webbrowser control on my form.
  • The user logs on to facebook through this webbrowser control.
  • After the logon, I start the authentication procedure.
  • I then retreive a code.

Here's where it goes wrong. With this code I want to obtain an access token. The generated request URL looks like: https://graph.facebook.com/oauth/access_token?client_id=____MY_APP_ID______&redirect_uri=http://localhost/&client_secret=_____MY_APP_SECRET_____&code=____MY_RETREIVED_CODE_____ and is made through the code below.

Please note that my redirect URL is http://localhost. This should be okay, right?

Also, in my App Settings, I have the following information.

Site URL: http://localhost/ Site Domain: localhost

private String ExchangeCodeForToken(String code, Uri redirectUrl)
{
    var TokenEndpoint = new Uri("https://graph.facebook.com/oauth/access_token");
    var url = TokenEndpoint + "?" +
                      "client_id=" + _AppID + "&" +
                      "redirect_uri=" + redirectUrl + "&" +
                      "client_secret=" + _AppSecret + "&" +
                      "code=" + code;
    var request = WebRequest.CreateDefault(new Uri(url));
    using (var response = request.GetResponse())
    {
        using (var responseStream = response.GetResponseStream())
        {
            using (var responseReader = new StreamReader(responseStream))
            {
                var responseText = responseReader.ReadToEnd();
                var token = responseText.Replace("access_token=", "");
                return token;
            }
        }
     }
}

When I execute this, I get this error:

error http://www.imageupload.org/getfile.php?id=50131&a=447f6fcc0ebd4d3f8e8a59a3a6e36ac3&t=4de0841c&o=0889D68FDC35508BA2C6F2689FCBAB7C30A8670CC9647EE598701D8BEC13ED278F0989D393&n=autherror.png&i=1

Webexception was unhandled by user code The remote server returned an error: (400) Bad Request.

Here's where I think I might be going wrong:

  • Are my app settings correct?
  • Should my redirect url be http://localhost, even if there isn't actually a service listening there?

Most imp开发者_如何学Pythonortantly:

  • how do I get rid of this error and retreive the access token?

Thanks in advance!


You get this error because you are not supposed to call this URL from a Desktop app : as far as I know, you can not use the token endpoint for Desktop app authentication. Also, you can get the access token directly (no need to ask for a code first). Here is what you have to do.

Load the following URL in your embedded web browser :

https://www.facebook.com/dialog/oauth?
  client_id=YOUR_APP_ID&
  redirect_uri=https://www.facebook.com/connect/login_success.html

The user will be asked to log in and will be redirected to this URL with the access token in the URL :

https://www.facebook.com/connect/login_success.html#access_token=...

So you have to detect the redirect and retrieve the access token from the URL.


Thanks quinten!

However, I've managed to solve my own problem by using the C# Facebook SDK. This software development kit has been a really great help!

There are a lot of samples included (including authorisation)

Anyone who programs in .NET with facebook should check it out! Coding for facebook is now much easier.

http://facebooksdk.codeplex.com/

0

精彩评论

暂无评论...
验证码 换一张
取 消