从Facebook获取长实时访问令牌

时间:2012-05-06 01:06:11

标签: facebook-graph-api

据我了解,最近Facebook决定删除offline_access权限,并引入了一个名为长期访问令牌的概念,最长可达60天。有谁知道如何使用Facebook JavaScript SDK获取此访问令牌?

4 个答案:

答案 0 :(得分:107)

有一种方法可将此延长至60天。这里描述:https://developers.facebook.com/docs/roadmap/completed-changes/offline-access-removal/Scenario 4: Client-side OAuth and Extending Access_Token Expiration Time through New Endpoint

编辑: 为了扩展访问令牌,您需要使用短期访问令牌发出以下请求:

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

答案 1 :(得分:12)

由于Facebook存在漏洞,一些用户必须在Facebook发布长期令牌之前取消对该应用的授权。

答案 2 :(得分:0)

我刚刚使用'axios'进行了Facebook Graph API调用。您可以从App Dashboard中找到client_id和client_secret。

getLongLiveToken = () => {
    window.FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            let userAccessToken = response.authResponse.accessToken;
            axios.get(`https://graph.facebook.com/oauth/access_token?client_id=${clientId}&client_secret=${clientSecret}&grant_type=fb_exchange_token&fb_exchange_token=${userAccessToken}`)
            .then((response) => {
                console.log("Long Live Access Token");
                console.log(response.data.access_token);
             });
           }
       });
    }
<button onClick={ () => this.getLongLiveToken() } >Long Live Token</button>

答案 3 :(得分:-1)

使用以下详细信息为javascript添加功能:我希望它适合您。

function getLongLiveToken(data){

        FB.api('oauth/access_token', {
            client_id: data.client_id, // FB_APP_ID
            client_secret: data.secret, // FB_APP_SECRET
            grant_type: 'fb_exchange_token',
            fb_exchange_token: data.access_token // USER_TOKEN
        }, function (res) {
            if(!res || res.error) {
                console.log(!res ? 'error occurred' : res.error);
            }else{
                var accessToken = res.access_token;
                if(typeof accessToken != 'undefined'){
                }
            }
        });
    }
相关问题