0

I make a HTTP GET request to Facebook to have a long live token , as a response I have a plain text with the access token and expiration date , but I don't know how to parse it.

request.get('https://graph.facebook.com/oauth/access_token?client_id=' + APP_ID + '&client_secret=' + APP_SECRET + '&grant_type=fb_exchange_token&fb_exchange_token=' + CURRENT_ACCESS_TOKEN)
    .on('data', function(data) {
        console.log("body= " + data); 
    });

res.render('SocialMedia.ejs');

I tried data.access_token but it's undefined

Jyotman Singh
  • 10,792
  • 8
  • 39
  • 55
Sam
  • 2,673
  • 2
  • 15
  • 20
  • can you post the output of your `console.log` statement? – Jyotman Singh Mar 13 '17 at 18:40
  • access_token=(the token)&expires=5166486 – Sam Mar 13 '17 at 18:42
  • Can you try with following url - `https://graph.facebook.com/v2.8/oauth/access_token`? Notice that I've just included the version `2.8`. Now please check the response. – Jyotman Singh Mar 13 '17 at 18:58
  • `body= {"error":{"message":"Missing redirect_uri parameter.","type":"OAuthException","code":191,"fbtrace_id":"F6gESsfgJr\/"}}` I going to search how to solve this uri problem – Sam Mar 13 '17 at 19:24

1 Answers1

0

Go through this documentation to get working with the lates 2.8 API (recommended). This will return a JSON response.

If you want to continue using your API then to parse your response which is in the form of url query params -

access_token=(the token)&expires=5166486 // data

You can do this -

var qs = require('qs');
var response = qs.parse(data); // assuming data is as mentioned above
console.log(response); // will print {access_token: (the token), expires: 5166486}

Now you can access the token like this -

response.access_token; 
Jyotman Singh
  • 10,792
  • 8
  • 39
  • 55
  • Thank you but I still have "undefined" in console. `request.get('https://graph.facebook.com/oauth/access_token?client_id=' + APP_ID + '&client_secret=' + APP_SECRET + '&grant_type=fb_exchange_token&fb_exchange_token=' + CURRENT_ACCESS_TOKEN) .on('data', function(data) { var response = qs.parse(data); console.log("body= " + response.access_token); }); res.render('SocialMedia.ejs');` – Sam Mar 13 '17 at 19:33
  • Yay ! It's working , it's just needed to write this `Data = data.toString();` to don't have the "undefined" response , thank you very much ! – Sam Mar 13 '17 at 19:53