1

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):

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

Call to get number of friends :(node.js)

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

Method to fetch number of friends:(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:

enter image description here

1
  • You are messing up the query string. It starts with a question mark, and then uses an ampersand to separate the name=value pairs from each other. Commented Jan 19, 2017 at 9:22

1 Answer 1

1

Did 2 things wrong:

  1. Improper URL , second parameter that was being passed was without '&' in URL.

  2. When using https.get with 'options' like this call:

    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. eg: /v2.8/me?fields=friends

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.