1

我正在从 Twilio 函数调用 Twilio Studio Flow Rest API。

以下是功能代码:

exports.handler = function(context, event, callback) {
   const request = require('request');  
   to = event.to;
   to = to.replace(/[()-.]/g, '');
   to = to.replace(/ /g, '');
   var postoptions = {  
   headers: {'AC' : 'b1xx'},       
   url:    'https://studio.twilio.com/v1/Flows/FWxxx8/Executions',  
        method: 'POST',  
        data: { 'from':  '+1814xxx',  
                'to': to 
            }  
        };

         request(postoptions, function(error, response, body){  
             callback(null, response);
         });
};

该函数一直说帐户 SID 和身份验证令牌不正确。但是 sid 和 token 是正确的。

我错过了什么?

4

1 回答 1

0

Account SID and auth token当您在标头中传递它们时,您需要对它们进行“base64”编码。

尝试这样的事情:


exports.handler = function (context, event, callback) {
    const request = require('request');
    to = event.to;
    to = to.replace(/[()-.]/g, '');
    to = to.replace(/ /g, '');

    var username = "AC";
    var password = "b1xx";
    var auth = "Basic " + new Buffer(username + ":" + password).toString("base64");

    var postoptions = {
        headers: { 'Authorization': auth },
        url: 'https://studio.twilio.com/v1/Flows/FWxxx8/Executions',
        method: 'POST',
        data: {
            'from': '+1814xxx',
            'to': to
        }
    };

    request(postoptions, function (error, response, body) {
        callback(null, response);
    });
};

于 2019-03-18T16:51:58.477 回答