【问题标题】:How to make GET request inside a GET request in NodeJS using Express如何使用 Express 在 NodeJS 的 GET 请求中发出 GET 请求
【发布时间】:2018-08-25 09:55:05
【问题描述】:

基本上,我试图在我的回调 GET 方法中从 Facebook 获取访问令牌。下面是我的代码。

getAccessToken 根本没有被调用。实施它的正确方法是什么?

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');

   });


});

【问题讨论】:

  • 尝试从像 req.body.code 这样的 body 中获取参数
  • 显示您在控制台中遇到的错误
  • 这看起来很奇怪...为什么在 app.get 中使用 app.use?你应该看看文档:expressjs.com/de/guide/using-middleware.html
  • 你不应该使用 app.use 在里面接听电话。用于中间件
  • 也许您可以尝试使用request 模块并充当HTTP客户端?

标签: node.js facebook express


【解决方案1】:

你不应该在里面使用app.use来接听电话。

您必须尝试执行以下操作。在 get 调用中进行另一个 get 调用以获取令牌。

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.
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-04
    • 2020-01-15
    • 1970-01-01
    • 2016-10-21
    • 2011-06-18
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    相关资源
    最近更新 更多