Vivek Mohan
Vivek Mohan

Reputation: 8366

Facebook access_token failure from node.js

I got this error while trying to get facebook access_token from node.js server:

{"error":{"message":"(#803) Some of the aliases you requested do not exist : access_token","type":"OAuthException","code":803}}

Following is the code for getting access_token:

var options={
        host:'graph.facebook.com',      
        path:'oauth/access_token?client_id=MYAPPID&redirect_uri=http://127.0.0.1:8000/&client_secret=MYAPPSECRET&code=CODEOBTAINEDFROMFB'
}
http.get(options,function(res){
    res.on('data', function (chunk) {
        console.log('BODY: ' + chunk);
    });
});

Request for the same url from my browser is working fine and I am getting the access_token also. what is the prob here?

Update This may be the most funniest thing I have experienced from node.js. I have updated the above code as:

var http=require('http')
var options={
        host:'grah.facebook.com',       
        pathname:'/oauth/access_token',
        search:'client_id=MYAPPID&redirect_uri=http://127.0.0.1:8000/&client_secret=MYAPPSECRET&code=CODEOBTAINEDFROMFB'
}

http.request(options,function(res){
        console.log('http fb code')
         res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log('BODY: '+chunk);
    });     
});

And what happening is server throw an error called socket hang up. Also my network connection(internet connected via mobile modem) breaks out immediately! While Googling this issue I found a lot similar to this, but can't find the perfect solution...

Upvotes: 3

Views: 1245

Answers (2)

Vivek Mohan
Vivek Mohan

Reputation: 8366

The problem is with http object. Facebook only allows https while requesting with client_secret. so this can work as I expected:

var options={
        host:'graph.facebook.com',      
        path:'/oauth/access_token?client_id=APPID&redirect_uri=http://127.0.0.1:8000/&client_secret=CLIENTSECRET&code=FBUSERCODE'
}       
var https=require('https'); 
https.get(options,function(res){
        console.log('http fb code')
         res.setEncoding('utf8');       
         res.on('data', function (chunk) {
                console.log('BODY: '+chunk);
        });
});         

Upvotes: 0

Juicy Scripter
Juicy Scripter

Reputation: 25938

HTTP documentation lists path as option but states:

options align with url.parse()

You should use pathname (and search for query string) instead of path while creating your URL. Also see URL in documentation for node.js

var options={
  host:'graph.facebook.com',      
  pathname:'oauth/access_token',
  search: 'client_id=MYAPPID&redirect_uri=http://127.0.0.1:8000/&client_secret=MYAPPSECRET&code=CODEOBTAINEDFROMFB'
}
http.get(options,function(res){
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

Update:
In description of URL object in url.format() documentation doesn't list path as option so it's probably only returned by url.parse()

Node documentation on URL states:

path: Concatenation of pathname and search.

Upvotes: 2

Related Questions