【问题标题】:facebook long-live access using DotNetOpenAuth使用 DotNetOpenAuth 的 facebook 长期访问
【发布时间】:2014-07-28 10:36:13
【问题描述】:

我正在 asp.net 中创建一个应用程序,它将使用用户的 facebook 数据使用其 api, 但为了访问 Fb api,我们需要先执行 OAuth(2.0)。 我也通过在以下代码中使用 DotNetOpenAuth 库来做到这一点:

private static readonly FacebookClient client = new FacebookClient
    {
        ClientIdentifier = ConfigurationManager.AppSettings["fbAppID"],
        ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(ConfigurationManager.AppSettings["fbSecret"]),
    };

    protected void Page_Load(object sender, EventArgs e)
    {
        IAuthorizationState authorization = client.ProcessUserAuthorization();
        if (authorization == null)
        {
            // Kick off authorization request
            client.RequestUserAuthorization();                
        }
        else
        {
            var req = WebRequest.Create("https://graph.facebook.com/oauth/access_token?client_id=" + ConfigurationManager.AppSettings["facebookAppID"] + "&client_secret=" + ConfigurationManager.AppSettings["facebookAppSecret"] + "&grant_type=fb_exchange_token&fb_exchange_token=" + Uri.EscapeDataString(authorization.AccessToken));


            var request = WebRequest.Create("https://graph.facebook.com/me?access_token=" + Uri.EscapeDataString(authorization.AccessToken));
            using (var response = request.GetResponse())
            {
                using (var responseStream = response.GetResponseStream())
                {
                    var graph = FacebookGraph.Deserialize(responseStream);
                    this.nameLabel.Text = HttpUtility.HtmlEncode(graph.Name);
                }
            }
        }
    }

我的问题是,如何使用 DotNetOpenAuth 库获取 facebook 的长期访问令牌,这样用户就不需要一次又一次地登录,我可以将它存储在某个地方。 请帮帮我。

【问题讨论】:

  • 我也向这个 url 发送了一个请求:graph.facebook.com/oauth/access_token?client_id=" + ConfigurationManager.AppSettings["fbAppID"] + "&client_secret=" + ConfigurationManager.AppSettings["fbAppSecret"] + "&grant_type=fb_exchange_token&fb_exchange_token= " + Uri.EscapeDataString(authorization.AccessToken) 但我收到一个错误,即无效字符 'a',这很奇怪

标签: facebook facebook-graph-api dotnetopenauth


【解决方案1】:

如果可能,请使用 Facebook SDK for .Net 长时间创建访问令牌。可能是 60 天。

使用 Facebook;

var cl = new FacebookClient(short_lived_access_token);

            dynamic result = client.Post("oauth/access_token", new
            {
                client_id = "your app id",
                client_secret = "your app secret",
                grant_type = "fb_exchange_token",
                fb_exchange_token = client.AccessToken
            });

var long_lived_access_token = result.access_token;

【讨论】:

    猜你喜欢
    • 2014-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    相关资源
    最近更新 更多