Facebook Long Live访问令牌JavaScript SDK

时间:2018-01-05 03:04:41

标签: javascript facebook facebook-graph-api

我正在尝试为Facebook生成长期访问令牌,并提到Stack Overflow上的几个问题。无论如何实施图谱API调用,我都会收到错误消息:

Uncaught SyntaxError: Unexpected token :

错误对象:

{type: "http", message: "unknown error"}

但是在源选项卡中我得到了我的万岁访问令牌,但问题是我在控制台选项卡中收到错误。

getLongLiveToken = () => {
        window.FB.getLoginStatus((response) => {
            if (response.status === 'connected') {
                const accessToken = response.authResponse.accessToken;
                // console.log(accessToken);
                let a = "LLT:";
                console.log(a+response);
                const params = {
                    grant_type: 'fb_exchange_token',
                    client_id: 'client_id',
                    client_secret: 'client_secret',
                    fb_exchange_token: accessToken
                };
                window.FB.api('/oauth/access_token','get',params,(res) => {
                    console.log(res);
                })
            }
        });
    };

<button onClick={ () => this.getLongLiveToken() } >Long Live Token</button>

1 个答案:

答案 0 :(得分:-1)

我刚刚使用'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>