Facebook Javascript SDK扩展访问令牌获取“无效的OAuth访问令牌” - 190 - “OAuthException”

时间:2012-06-20 15:56:33

标签: facebook-graph-api oauth facebook-javascript-sdk facebook-access-token

我尝试使用javacript SDK获取访问令牌,然后将该访问令牌延长60天。我从响应中得到了这样的响应错误:Object { message="Invalid OAuth access token.", type="OAuthException", code=190}

我的期望:

  1. 获取新的访问令牌,有效期为60天
  2. 控制台到屏幕。
  3. 我的代码:

    window.onload = function() {
    
    var isLogin = true;
    
    FB.init({appId:422642254433770, cookie:true, status:true, xfbml:true });
    
    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            // the user is logged in and connected to your
            // app, and response.authResponse supplies
            // the user’s ID, a valid access token, a signed
            // request, and the time the access token 
            // and signed request each expire
            var uid = response.authResponse.userID;
            var accessToken = response.authResponse.accessToken;
            var accessTokenOld = response.authResponse.accessToken;
    
            //Extend access token                    
            var OauthParams = {};
            OauthParams['client_id'] = '//REMOVED APP ID';
            OauthParams['client_secret'] = '//REMOVED APP SECRET';
            OauthParams['grant_type'] = 'fb_exchange_token';
            OauthParams['fb_exchange_token'] = 'accessToken';
            OauthParams['response_type'] = 'token';
    
            console.log("Old accessToken => " + accessToken);
            FB.api('/oauth/access_token', 'post', OauthParams, function(response) {
                console.log(response);
    
                if (!response || response.error) {
                    console.log(response.accesstoken);
                } else {
                    console.log("Lay new access token bi loi " + response.error.message);
                }
            });        
        }
    });
    };
    

    我试着在没有任何线索的情况下在3天内搜索这个问题。我有没有经验?请帮忙。

    非常感谢

2 个答案:

答案 0 :(得分:1)

在客户端中执行此操作是一个坏主意,因为需要在实际客户端中包含应用程序机密,这非常危险。最好在服务器上调用一个单独处理此端点的端点,以便应用程序机密保留在受控环境中。

答案 1 :(得分:-2)

我找到了一个简单的解决方案,使用Jquery valide获取扩展访问令牌长达2个月

要传递给网址的变量:

var accessToken = 'CURRENT_ACCESS_TOKEN';
var appid       = 'APPID';
var appsecret   = 'APPSECRET';

您的网址应指向

var exchangeUrl = "https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&fb_exchange_token="+accessToken+"&client_id="+appid+"&client_secret="+appsecret;

的形式发出Ajax请求
$.ajax({  
type: "GET",
url: exchangeUrl,  
success: function(data)
{ 
   extended = data.split('=');
   extendedAT = extended['1'].replace('&expires','');
       alert(extendedAT);

}

});