简体   繁体   English

无法使用 node.js 与 Facebook 的图形 API 交互

[英]Unable to interact with facebook's graph API using node.js

This is the url I am trying to hit:这是我试图点击的网址:

https://graph.facebook.com/v2.8/me?fields=friends?access_token=xxxx.

Expected result : (as on Graph API Explorer):预期结果:(如在 Graph API Explorer 上):

{
  "friends": {
    "data": [
    ],
    "summary": {
      "total_count": 648
    }
  },
  "id": "xxxx"
}

Call to get number of friends :(node.js)调用以获取朋友数量:(node.js)

fb.getFbData(constants.AccessToken, 'me?fields=friends', function(data){
        res.send(data);
    });

Method to fetch number of friends:(node.js)获取好友数量的方法:(node.js)

var https = require('https');
    exports.getFbData = function(accessToken, apiPath, callback) {
        var options = {
            host: 'graph.facebook.com',
            port: 443,
            path: apiPath + '?access_token=' + accessToken, //apiPath example: '/me/friends'
            method: 'GET'
        };

        console.log("\n\n options:::", options);
        var buffer = ''; //this buffer will be populated with the chunks of the data received from facebook
        var request = https.get(options, function(result){
            result.setEncoding('utf8');
            result.on('data', function(chunk){
                buffer += chunk;
            });

            result.on('end', function(){
                callback(buffer);
            });
        });

        request.on('error', function(e){
            console.log('error from facebook.getFbData: ' + e.message)
        });

        request.end();
    }

Error:错误:

在此处输入图片说明

Did 2 things wrong:做错了2件事:

  1. Improper URL , second parameter that was being passed was without '&' in URL. URL 不正确,传递的第二个参数在 URL 中没有“&”。

  2. When using https.get with 'options' like this call:当使用 https.get 和“选项”这样的调用时:

    var options = { host: 'graph.facebook.com', port: 443, path: apiPath + '?access_token=' + accessToken, method: 'GET' }; var options = { host: 'graph.facebook.com', port: 443, path: apiPath + '?access_token=' + accessToken, method: 'GET' };

Then, 'host' gets appended in the URL and therefore the 'path' only needs to contain the directory on the website.然后,“主机”被附加到 URL 中,因此“路径”只需要包含网站上的目录。 eg: /v2.8/me?fields=friends例如:/v2.8/me?fields=friends

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM