代码之家  ›  专栏  ›  技术社区  ›  arun kumar

Twilio函数调用Studio Rest API-Accountsid时出现正确错误

  •  0
  • arun kumar  · 技术社区  · 7 年前

    我正在从Twilio函数调用Twilio Studio流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);
             });
    };
    

    我错过了什么?

    0 回复  |  直到 7 年前
        1
  •  0
  •   Alex Baban    7 年前

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

    试着这样做:

    
    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);
        });
    };
    
    
    推荐文章