首页 文章

无法使用facebook graph api获取长期访问令牌

提问于
浏览
16

我按照以下说明工作并尝试获取长期访问令牌:https://developers.facebook.com/docs/facebook-login/access-tokens/即使在此处使用Graph API资源管理器:https://developers.facebook.com/tools/explorer/我输入以下内容并使用我的AppID和AppSecret以及当我获得的当前令牌填充它按下获取访问令牌...

GET / oauth / access_token? grant_type = fb_exchange_token&client_id = &client_secret = &fb_exchange_token =

我得到了回报

{“错误”:“无效回复”}

有人可以详细说明我可能做错了什么,或者更详细的步骤,以便您获得这个长期令牌 .

我在这个帖子中发生了've tried to follow what',没有更多的成功 . 任何帮助将不胜感激 .

谢谢你的时间和帮助 . 干杯,

-Ryan

4 回答

  • 1

    您无法使用 Graph API Explorer 获取 long-lived user token . 您必须发出 GET 请求:

    https://graph.facebook.com/oauth/access_token?
    grant_type=fb_exchange_token&
    client_id={app-id}& client_secret={app-secret}& fb_exchange_token={short-lived-token}
    

    您可以在浏览器中查看它 .

    如果您需要 page access token ,则可以使用 never 到期令牌 . 在这里查看接受的答案:What are the Steps to getting a Long Lasting Token For Posting To a Facebook Fan Page from a Server

  • 10

    所以我想我会重新审视这个并提供我写的文档,希望能帮助其他人实现这一目标!

    ONE . 创建应用程序

    创建与您要访问的页面用户关联的应用程序 .

    TWO . 获取所需的信息片段

    创建应用程序后,我们应该有两个关键信息:

    应用ID:AAAAA(长度约为15个字符)

    App Secret:BBBBB(大约应该约32个字符)

    有了这些,请转到https://developers.facebook.com/tools/explorer确保从顶部的下拉框中选择正确的应用程序 .

    单击获取访问令牌并获得“新鲜”令牌 .

    在这里,您需要为特定应用程序的目的选择适当的权限 .

    CCCCC(长度应约200个字符)

    THREE . 获得长寿命令(2个月)

    然后,您应该拥有运行查询以获取长期(2个月)令牌所需的信息:

    https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id= &client_secret = &fb_exchange_token =

    替换为您目前已注意到的三位信息 .

    您应该收到如下请求:

    https://graph.facebook.com/oauth/access_token?%20grant_type=fb_exchange_token&%20client_id=AAAAA&%20client_secret=BBBBB&%20fb_exchange_token=CCCCC

    将此查询放入互联网浏览器的网址栏 . 您应该在窗口中看到如下所示的响应:

    =的access_token&DDDDD到期= 5184000

    DDDDD(应该长约200个字符)

    FOUR . 测试令牌(第1部分)

    如果将突出显示的部分输入到以下调试站点的输入中:

    https://developers.facebook.com/tools/debug/

    它应该给你约2个月的到期时间 .

    FIVE . 获取非过期页面令牌

    现在注意这个新的长实时令牌,我们将使用它来获取一个不会过期的令牌,除非关联的应用程序从用户的访问中删除或删除 . 在发出请求时,我们使用页面名称或最好使用page-id:

    您可以使用类似http://findmyfacebookid.com/的内容获取您的Facebook页面ID . 我们将您的页面ID称为EEEEE

    https://graph.facebook.com/ /?fields = access_token&access_token =

    您应该收到如下请求:

    https://graph.facebook.com/EEEEE/?fields=access_token&access_token=DDDDD

    这将返回如下内容:{“access_token”:“FFFFF”,“id”:“131062838468”}

    FFFFF(长度应为~200个字符)

    SIX . 测试令牌(第2部分)

    获取突出显示的部分并将其输入调试页面,您应该得到一些显示令牌永不过期的内容,并且您已成功获取永不过期的页面令牌 .

    SEVEN . 举手击掌!

    对于如何实现这一目标的长列表感到抱歉,但我发现最好只给出整个过程而不只是一个小片段 . 如果您发现这有用,或者您有更好的方法来实现任何步骤,请告诉我们 .

  • 22

    Facebook PHP SDK已经实现了使用短期令牌获取长期令牌的方法,登录成功后获得了短期令牌,简单的调用

    $result = $facebook->setExtendedAccessToken();
    

    如果$ result为null,则表示您获得了长期访问令牌 .

  • 0

    客户端

    if (response.status === 'connected') {
                {
                    event.preventDefault();
                    FB.login(function (response) {
                        if (response.authResponse) {
                            var profileId = response.authResponse.userID;
                            var accessToken = response.authResponse.accessToken;
                            var e = response.authResponse.accessToken;
                            document.getElementById('token').innerHTML = e;
                            var profileName = "";
                            var pagesList = "";
                            var isPage = 0;
                            var type = "fb";
                            $.ajax({
                                url: "WebService1.asmx/getlonToken",
                                type: "POST",
                                dataType: 'json',
                                data: '{accessToken:"' + accessToken + '"}',
                                contentType: "application/json; charset=utf-8",
                                async: true,
                                success: function (response) {
                                    accessToken = response.d;
                                    document.getElementById('status').innerHTML = accessToken;
                                },
                                error: function (e) {
                                    alert('Error' + e);
                                }
                            });
    
    
    
                        }
                    }, { scope: 'user_about_me,friends_about_me,user_activities,friends_activities,user_birthday,friends_birthday,user_education_history,friends_education_history,user_events,friends_events,user_groups,friends_groups,user_hometown,friends_hometown,user_interests,friends_interests,user_likes,friends_likes,user_location,friends_location,user_notes,friends_notes,user_photos,friends_photos,user_relationships,friends_relationships,user_relationship_details,friends_relationship_details,user_status,friends_status,user_videos,friends_videos,user_website,friends_website,email,manage_pages,publish_stream,read_stream,read_page_mailboxes,read_insights, read_mailbox' });
    
                }
    

    服务器端

    [WebMethod]
        public string getlonToken(string accessToken)
        {
            var fb = new FacebookClient(accessToken);
            dynamic result = fb.Get("oauth/access_token", new
                {
                    client_id = 123,
                    client_secret = "123fff45",
                    grant_type = "fb_exchange_token",
                    fb_exchange_token = accessToken,
                    scope = "user_about_me,friends_about_me,user_activities,friends_activities,user_birthday,friends_birthday,user_checkins,friends_checkins,user_education_history,friends_education_history,user_events,friends_events,user_groups,friends_groups,user_hometown,friends_hometown,user_interests,friends_interests,user_likes,friends_likes,user_location,friends_location,user_notes,friends_notes,user_photos,friends_photos,user_relationships,friends_relationships,user_relationship_details,friends_relationship_details,user_religion_politics,friends_religion_politics,user_status,friends_status,user_videos,friends_videos,user_website,friends_website,email,manage_pages,publish_stream,read_stream,read_page_mailboxes,read_insights,ads_management"
                });
    
            fb.AccessToken = (string)result["access_token"];
            return fb.AccessToken;
        }
    }
    

相关问题