代码之家  ›  专栏  ›  技术社区  ›  Ennio

如何从Azure函数进行https调用?

  •  1
  • Ennio  · 技术社区  · 6 年前

    我正在尝试使用nodeJS创建Azure函数,但是当我调用HTTPSAPI时会收到一条错误消息。

    这是我的密码

    const https = require('https');
    const querystring = require('querystring');
    
    module.exports = async function (context, req) {
    
        if (req.query.accessCode || (req.body && req.body.accessCode)) {
    
            var options = {
                host: 'api.mysite.com',
                port: 443,
                path: '/oauth/access_token',
                method: 'POST'
            };
    
            var postData = querystring.stringify({
                client_id : '1234',
                client_secret: 'xyz',
                code: req.query.accessCode
            });
    
    
            var req = https.request(options, function(res) {
                context.log('STATUS: ' + res.statusCode);
                context.log('HEADERS: ' + JSON.stringify(res.headers));
    
                res.on('data', function (chunk) {
                    context.log('BODY: ' + chunk);
                });
            });
    
            req.on('error', function(e) {
                context.log('problem with request: ' + e.message);
            });
    
            req.write(postData);
            req.end();
    
            context.res = {
                status: 200,
                body: "Hello " + (req.query.accessCode)
            };
        } else {
           context.res = {
                status: 400,
               body: "Please pass a name on the query string or in the request body"
          };
        }
    
        context.done();
    };
    

    我得到一个错误,但我没有看到控制台上的任何错误,如果我评论所有的https调用它的工作正常,我可以看到屏幕上的Hello消息。

    1 回复  |  直到 6 年前
        1
  •  4
  •   Community CDub    4 年前

    两点需要修正

    1. context.done(); . 看到了吗 Azure document .

    2. 将你的https.request重命名为 var myReq = https.request(options, function(res)

      由于函数具有内置的 req 声明的对象。

        2
  •  0
  •   aderesh    4 年前

    var http = require('https');
    
    module.exports = function (context, req) {
        var body = "";
        body += 'grant_type=' + req.query['grant_type'];
        body += '&client_id=' + req.query['client_id'];
        body += '&client_secret=' + req.query['client_secret'];
        body += '&code=' + req.query['code'];
    
        const options = {
            hostname: 'login.microsoftonline.com',
            port: 443,
            path: '/ZZZ920d8-bc69-4c8b-8e91-11f3a181c2bb/oauth2/v2.0/token',
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Length': body.length
            }
        }
    
        var response = '';
        const request = http.request(options, (res) => {
            context.log(`statusCode: ${res.statusCode}`)
    
            res.on('data', (d) => {
                response += d;
            })
    
            res.on('end', (d) => {
                context.res = {
                    body: response
                }
                context.done();
            })
        })
    
        request.on('error', (error) => {
            context.log.error(error)
            context.done();
        })
    
        request.write(body);
        request.end();
    };
    

    区别在于-函数不是异步的 module.exports = function

    我相信你的问题是:

    link