How to make GET request inside a GET request in NodeJS using Express

别等时光非礼了梦想. 提交于 2020-01-04 05:36:14

问题


Basically, I'm trying to get Access Token from Facebook in my callBack GET method. Below is my code.

getAccessToken is not called at all. What's the right way to implement it?

app.get('/fbcallback', function(req, res) {


  var code = req.query.code;

  var getAccessToken =  'https://graph.facebook.com/v2.12/oauth/access_token?'+
   'client_id='+client_id+
   '&redirect_uri='+redirect_uri+
   '&client_secret='+client_secret+
   '&code='+code;


   app.use(getAccessToken, function(req, res) {

        console.log('Token Call');

   });


});

回答1:


You should not use app.use inside get the call.

You must be trying to do something like below. Inside get call make another get call for getting token.

var request = require('request');

app.get('/fbcallback', function (req, res) {
    var code = req.query.code;
    var getAccessToken = 'https://graph.facebook.com/v2.12/oauth/access_token?' +
        'client_id=' + client_id +
        '&redirect_uri=' + redirect_uri +
        '&client_secret=' + client_secret +
        '&code=' + code;

    request(getAccessToken, function (error, response, body) {
        console.log('error:', error); // Print the error if one occurred
        console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
        console.log('body:', body); // Print the HTML for the Google homepage.
    });
});


来源:https://stackoverflow.com/questions/49318921/how-to-make-get-request-inside-a-get-request-in-nodejs-using-express

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!