Facebook 离线访问 .NET SDK

发布于 2024-11-05 16:37:16 字数 1352 浏览 0 评论 0原文

我正在开发 Facebook 应用程序。我正在从我的应用程序用户那里获取离线访问权限。现在我希望我可以使用用户第一次使用我的应用程序时生成的相同令牌,并且我会将此令牌存储在我的数据库中,并且每当用户再次登录我们的网站并希望通过我们的应用程序发布一些墙贴时我们将使用相同的令牌在墙上发布。

这是我的代码

protected void Page_Load(object sender, EventArgs e)
{
    var auth = new CanvasAuthorizer { Permissions = new[] { "user_about_me", "publish_stream", "video_upload", "share_item", "photo_upload", "offline_access" } };

    var fbWebContext = FacebookWebContext.Current;
    if (fbWebContext.IsAuthorized())
    {
        try
        {
            var fb = new FacebookWebClient(fbWebContext);
            var token = fb.AccessToken;  \\ Im Getting Token This Way When its Generated From User After Getting Extended Permission
            dynamic result = fb.Get("/me");
            long id = fbWebContext.UserId;
        }
        catch (Exception ex)
        {
            if ("(OAuthException) Error validating access token: The session is invalid because the user logged out or because auth.expireSession was invoked." == ex.Message)
            {
                fbWebContext.DeleteAuthCookie();
                Session.Clear();
            }
        }
    }
}

,我使用 var token = fb.AccessToken 获取访问令牌,但是当我使用它时,它说

您的令牌已于 Unix 时间 xxxxxxxxxx 过期。

任何人都可以告诉我,如果这是请求离线访问的扩展权限后的访问令牌,那么为什么会显示“您的访问令牌已过期?”。有人可以帮忙吗?

I am working on facebook application. I am getting the offline access permission from my app user. Now I want that I can use the same tokens that generated one time when user uses first time my app and i will store this token in my db and whenever the user will login again on our site and want to publish some wall post through our app we will use the same token for publishing on wall.

Here is my code

protected void Page_Load(object sender, EventArgs e)
{
    var auth = new CanvasAuthorizer { Permissions = new[] { "user_about_me", "publish_stream", "video_upload", "share_item", "photo_upload", "offline_access" } };

    var fbWebContext = FacebookWebContext.Current;
    if (fbWebContext.IsAuthorized())
    {
        try
        {
            var fb = new FacebookWebClient(fbWebContext);
            var token = fb.AccessToken;  \\ Im Getting Token This Way When its Generated From User After Getting Extended Permission
            dynamic result = fb.Get("/me");
            long id = fbWebContext.UserId;
        }
        catch (Exception ex)
        {
            if ("(OAuthException) Error validating access token: The session is invalid because the user logged out or because auth.expireSession was invoked." == ex.Message)
            {
                fbWebContext.DeleteAuthCookie();
                Session.Clear();
            }
        }
    }
}

I get the access token using var token = fb.AccessToken but when I use it it says

Your Token is Expired at Unix Time xxxxxxxxxx.

Can anyone please tell me anyone if this is the access token after requesting the extended permission of offline access then why is it saying "Your Access token is Expired?". Can anyone please help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱没勇气 2024-11-12 16:37:16

这里的主要问题是,即使您请求离线访问权限,您也不能 100% 确定令牌永远不会过期。

在许多情况下,访问令牌可能会过期(即使使用离线访问),例如:用户更改密码、应用程序机密更改等。在这些情况和其他情况下,您的应用程序必须能够检测到无效的访问令牌并通过用户通过身份验证流程获取新的令牌。

您可以测试您的访问令牌,并使用访问令牌和访问令牌 lint 工具对其进行调试:https: //developers.facebook.com/tools/access_tokenhttps://developers.facebook.com/tools/access_token/lint

要仔细检查您是否已成功获得有效令牌,请将通过 API 获得的令牌复制并粘贴到图形浏览器中工具。 https://developers.facebook.com/tools/explorer/?method =GET&路径=我

The main issue here is that even though you request the offline_access permission, you can't be 100% sure the token will never exipre.

There are a number of cases where the Access Token may expire - even with the offline_access - scenarios such as: user changes password, app secret changes etc. In these and other cases, your app must be able to detect the invalidated access token and pass the user through the auth flow to get a new, fresh token.

You can test your access tokens, and get debug on them by using the access token and access token lint tools: https://developers.facebook.com/tools/access_token and https://developers.facebook.com/tools/access_token/lint

To double check you've successfully got a valid token, copy and paste one you got via the API into the graph explorer tool. https://developers.facebook.com/tools/explorer/?method=GET&path=me

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文