(Facebook C#SDK)获取访问令牌时出现问题 [英] (Facebook C# SDK) Problem getting an access token

查看:162
本文介绍了(Facebook C#SDK)获取访问令牌时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Facebook C#SDK(5.0.3)还是很陌生,这很可能是这个问题的原因. 基本上,我正在尝试获取当前用户的个人资料,电子邮件,照片等.下面您将在我的两个页面(MyLogin.aspx和Landingpage.aspx)中找到代码.我使用Web表单购买方式.

I'm quite new to the Facebook C# SDK (5.0.3) which probablly is the reason for this question. Basically, I'm trying to get the current users profile, email, photo, etc etc. Below you'll find the code to my two pages (MyLogin.aspx and landingpage.aspx). I use web forms buy the way.

第一页显示一个登录按钮,然后重定向到登录页面.有关更多信息,请参见代码中的注释.我遇到各种异常,我不知道该如何解决.

The first page displays a login button, and then redirects to the landingpage. See my comments in the code for further information. I get various exceptions I don't know how to solve.

如果您有任何允许我前进的指导,我对此深表感谢.

If you have any guidance that allows me to move forward, I am very grateful for that.

所以,这是代码...

So, heres the code ...

MyLogin.aspx.cs

MyLogin.aspx.cs

    protected void DoLogin(object sender, EventArgs e)
    {
        Response.Redirect(GetFacebookLoginUrl());
    }


    private static string GetFacebookLoginUrl()
    {
        try
        {
            const string baseUrl = "http://localhost:5000/";
            var extendedPermissions = new[] { "offline_access", "publish_stream" };

            var oauth = new FacebookOAuthClient
            {
                ClientId = FacebookContext.Current.AppId
            };

            //If I use token instead of code the FacebookOAuthResult.TryParse will return false.
            var parameters = new Dictionary<string, object>{{ "response_type", "code" },{ "display", "page" }};

            if (extendedPermissions != null && extendedPermissions.Length > 0)
            {
                var scope = new StringBuilder();
                scope.Append(string.Join(",", extendedPermissions));

                parameters["scope"] = scope.ToString();
            }
            parameters["redirect_uri"] = String.Format("{0}LandingPage.aspx", baseUrl);
            var url = oauth.GetLoginUrl(parameters).OriginalString;
            return url;
        }
        catch (Exception)
        {
            return string.Empty;
        }
    }

......这似乎是所有到达$的页面!@@ %% ...如果您知道我的意思.

... and here's the page where it all seems to got to $"!@@%% ... if you know what I mean.

Landingpage.aspx.cs

Landingpage.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        var cl = new FacebookOAuthClient(FacebookContext.Current);
        FacebookOAuthResult result = null;
        var url = Request.Url.AbsoluteUri;

        if (!FacebookOAuthResult.TryParse(url, out result)) return;
        if (result.IsSuccess)
        {
            //result.AccessToken is null, that's why I create a new instance of FacebookClient to get hold of the AccessToken.
            var accessToken1 = result.AccessToken;

            var app = new FacebookClient(FacebookContext.Current.AppId, FacebookContext.Current.AppSecret);
            var accessToken2 = app.AccessToken;

            //I now got an AccessToken but when I call client.Get("me");
            // I'll get (OAuthException) An active access token must be used to query information about the current user.

            //var client = new FacebookClient(accessToken2);
            //dynamic me = client.Get("me");
            //string firstName = me.first_name;
            //string lastName = me.last_name;
            //string email = me.email;

            // So, how do I get an active access token? 
            // Well, I did try using the FacebookOAuthClient object and the method ExchangeCodeForAccessToken (as you can see below).


            cl.ClientId = FacebookContext.Current.AppId;
            cl.ClientSecret = FacebookContext.Current.AppSecret;
            cl.RedirectUri = new UriBuilder("http://localhost:5000/").Uri;

            var parameters = new Dictionary<string, object> { { "permissions", "offline_access" } };
            var x = cl.ExchangeCodeForAccessToken(result.Code, parameters);

            //However, this now gives me the Exception (OAuthException) Error validating verification code.

        }
        else
        {
            var errorDescription = result.ErrorDescription;
            var errorReason = result.ErrorReason;
        }
    }

谢谢!

//尼克(Nicke)

// Nicke

推荐答案

我认为问题在于,您永远不会获得用户访问令牌,而只会获得应用程序访问令牌.在独立应用中,您需要使用oAuth对话框.最简单的方法是使用 JavaScript SDK .

I think the problem is that you never get a User Access Token you only get a App Access Token. In a standalone app you need to use the oAuth Dialog. The simplest way to do this is using the Javascript SDK.

Facebook C#SDK有一个示例,向您展示了如何做这.您可以下载整个示例应用程序(CSASPNETWebsite)作为起点.

Facebook C# SDK has a sample that shows you how to do this. You could download the whole sample app (CSASPNETWebsite) as a startingpoint.

这篇关于(Facebook C#SDK)获取访问令牌时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆