【问题标题】:ask FB api graph user info with access_token from my API OAuthException使用我的 API OAuthException 中的 access_token 询问 FB api graph 用户信息
【发布时间】:2018-04-08 01:12:38
【问题描述】:

我尝试从客户端应用程序提供的 access_token 将用户信息从我的 API 获取到 facebook API 图,但我不断收到此错误

{
 "error": {
    "message": "An active access token must be used to query information about the current user.",
    "type": "OAuthException",
    "code": 2500,
    "fbtrace_id": "***********"
 }
}

这是解释我的结构的图:

如果我做一个简单的事情会很奇怪: curl -i -H "Accept: application/json" -H "Content-Type: application/json" https://graph.facebook.com/v2.6/me?access_token=**ACCESS_TOKEN**&field=email

我对数据有一个有效的 json 答案。

但是当我从我的 API 中询问时,我得到了上面显示的错误... (将来我想检查数据库中是否有用户与 facebook 帐户使用相同的电子邮件,以避免双重帐户冲突)

我是这样做的:

var http = require("http");
var https = require("https");

/**
 * getJSON:  REST get request returning JSON object(s)
 * @param options: http options object
 * @param callback: callback to pass the results JSON object(s) back
 */
function getJSON(options, onResult)
{
    console.log("rest::getJSON");

    var port = options.port == 443 ? https : http;
    var req = port.request(options, function(res)
    {
        var output = '';
        console.log(options.host + ':' + res.statusCode);
        res.setEncoding('utf8');

        res.on('data', function (chunk) {
            output += chunk;
        });

        res.on('end', function() {
            var obj = JSON.parse(output);
            onResult(res.statusCode, obj);
        });
    });

    req.on('error', function(err) {
  //      res.send('error: ' + err.message);
    console.log(err.message + '\n');
    return(err);
    });

     req.end();
};

/*////////////////////////////
//                          //
//  Function /signup route  //
//                          //
*/////////////////////////////

// router.post('/signup-fb', signup-fb)
exports.signupFb = async (req, res) => {
  const data = req.body;

  console.log('login fb', data)

  if (!data.fbToken) {
      res.status(404);
      res.json({success: false, message: "Fb token is blank"});
      res.end();
      return
  }

  var options = {
    host: 'graph.facebook.com',
    port: 443,
    path: '/v2.6/me/',
    method: 'GET',
    body: {'access_token': data.fbToken,
            'fields': 'id,name,email'}
  };
  const tmp = await getJSON(options, function(statusCode, result) {
    // I could work with the result html/json here.  I could also just return it
    console.log(tmp);
    console.log("onResult: (" + statusCode + ")" + JSON.stringify(result));
    res.statusCode = statusCode;
    res.send(result);
});

【问题讨论】:

  • 您正在检查data.fbToken,但是您正在使用data.access_token ...?
  • 当我将代码放在 Stackoverflow 上时,我犯了一个错误,没有任何改变...
  • 不确定是否应该将其放入body - POST 请求确实有正文,但这是一个 GET ... 单独指定查询字符串,或将其附加到路径中。

标签: json facebook api facebook-graph-api oauth


【解决方案1】:

回答

答案在评论中。

要请求 API Graph,我必须在路径中附加正文的变量,如下所示:

var options = {
   host: 'graph.facebook.com',
   port: 443,
   path: '/v2.6/me/?access_token='+data.fbToken+'&fields=id,name,email',
   method: 'GET'
};

【讨论】:

    猜你喜欢
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多