【问题标题】:(Facebook C# SDK) Problem getting an access token(Facebook C# SDK)获取访问令牌时出现问题
【发布时间】:2011-03-11 13:23:21
【问题描述】:

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

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

如果您有任何指导可以让我继续前进,我非常感激。

所以,这里的代码...

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;
        }
    }

...如果您知道我的意思的话,这似乎是所有内容都指向 $"!@@%% ... 的页面。

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;
        }
    }

谢谢!!

// 尼克

【问题讨论】:

    标签: facebook-c#-sdk


    【解决方案1】:

    在代码请求和访问令牌请求中,redirect_uri 必须相同,这将最终修复 OAuthException。我的代码现在是这样的:

    ...    
    if (result.IsSuccess)
    {
        var tokenresult = cl.ExchangeCodeForAccessToken(result.Code, new Dictionary<string, object> { { "redirect_uri", "<your_redirect_uri>" } });
    }
    ...
    

    【讨论】:

      【解决方案2】:

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

      Facebook C# SDK 有一个 sample 向您展示如何执行此操作。您可以下载整个示例应用程序 (CSASPNETWebsite) 作为起点。

      【讨论】:

        【解决方案3】:

        您的代码大部分是正确的,唯一缺少的是调用“ExchangeCodeForAccessToken”方法时的“redirect_uri”参数。这必须与您在注册​​ Facebook 应用程序时设置的应用程序 URL 相匹配。否则,您将收到您收到的错误。您不需要“permissions”参数,您在事先授权期间已经收到。

         Dictionary<string, object> parameters = new Dictionary<string, object>();
         parameters.Add("redirect_uri", "http://www.yourcallbackurl.com/");
         var result = cl.ExchangeCodeForAccessToken(result.code,parameters);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-08-29
          • 1970-01-01
          • 2011-07-11
          • 1970-01-01
          • 1970-01-01
          • 2013-02-22
          • 2018-09-08
          相关资源
          最近更新 更多