1

我正在尝试运行 facebook checkToken 函数来确认用户。但是,我正在尝试弄清楚如何获取这部分所需的应用程序 access_token

GET graph.facebook.com/debug_token?
     input_token={token-to-inspect}
     &access_token={app-token-or-admin-token}

这是我的尝试:(但是我也设置了 access_token 的 tokendata 不能正常工作......如果有人能给我指导或告诉我如何解决这个问题,那将非常感谢!

var facebookCheckToken = function(login_token, callback) {

        var f_url = "https://graph.facebook.com/oauth/access_token?client_id=" + FBconfig.fb.client_id+ "&client_secret=" + FBconfig.fb.client_secret+" &grant_type=client_credentials";
        request(f_url, function(err, response, body) {
            if (err) {
                console.log("Bad fb login token request.", err.message);
                return callback(err);
            } else if (response.statusCode !== 200) {
                var error = JSON.parse(body).error;
                console.log("Bad facebook login token request: status code " + response.statusCode, error.message);
            }
        var tokendata = qs.parse(body).access_token;
        callback(null, tokendata);
        });
        var queryStr = qs.stringify({
            input_token: login_token,
            access_token: tokendata 
        });

        request('https://graph.facebook.com/debug_token?' + queryStr, function(err, response, body) {
            if (err) {
                console.log("Bad facebook token inspect request.", err.message);
            //  log.err("Bad facebook token inspect request.", err.message);
                return callback(err);
            } else if (response.statusCode !== 200) {
                var error = JSON.parse(body).error;
                console.log("Bad facebook token inspect request: status code " + response.statusCode, error.message);
                //log.err("Bad facebook token inspect request: status code " + response.statusCode, error.message);
                //return callback(err.message);
            }

            console.log("expires: " + tokendata.expires_at);
            console.log("timestamp: " + (new Date).getTime() / 1000);
            if (String(tokendata.app_id) !== FBconfig.fb.client_id) return callback(new Error("Facebook login token app id mismatch"));
            if (!tokendata.is_valid) return callback(new Error("Facebook login token is not valid"));
            if (tokendata.expires_at < (new Date).getTime() / 1000) return callback(new Error("Facebook login token has expired"));

            tokendata.access_token = login_token;
            return callback(null, tokendata);
        });
    };
4

0 回答 0