如何使用urllib2请求twilio或没有python sdk请求

时间:2017-11-07 16:37:54

标签: python python-requests twilio urllib2 twilio-api

我试图使用请求向Twilio api发出POST请求,但每次都返回401状态代码,问题是什么?

url = 'https://api.twilio.com/2010-04-01/Accounts/ACe3a9b......27812bc6/Messages'
values = {  
        'To' : '+XXXXX' , 
        'From' : '+XXXXXX' ,
        'Body' : 'XXXX' ,
        }

headers             = {'AuthToken' :'XXXXXXXXXXXXXXXXX' , 
                    'AccountSid' : 'XXXXXXXXXXXX' } 


response        = requests.post(url , data = values  , headers = headers )

谢谢

1 个答案:

答案 0 :(得分:0)

看起来像Twilio的REST API需要HTTP Basic Auth。放弃标题,然后将其替换为...

from requests.auth import HTTPBasicAuth
auth = HTTPBasicAuth('AccountSid', 'AuthToken')
response = requests.post(url, data = values, auth = auth)