2016-05-17 27 views
0

我跟着Udacity教程Twilio,我得到一個異常如下:Twilio - Python異常,同時發送短信 - Udacity教程

Traceback (most recent call last): 
    File "/Users/kaushiksekar/Documents/Programs/Python/send_text.py", line 10, in <module> 
    from_="+14066234282") # Replace with your Twilio number 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twilio-6.3.dev0-py2.7.egg/twilio/rest/resources/messages.py", line 122, in create 
    return self.create_instance(kwargs) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twilio-6.3.dev0-py2.7.egg/twilio/rest/resources/base.py", line 365, in create_instance 
    data=transform_params(body)) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twilio-6.3.dev0-py2.7.egg/twilio/rest/resources/base.py", line 200, in request 
    resp = make_twilio_request(method, uri, auth=self.auth, **kwargs) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twilio-6.3.dev0-py2.7.egg/twilio/rest/resources/base.py", line 164, in make_twilio_request 
    uri=resp.url, msg=message, code=code) 
TwilioRestException: 
[31m[49mHTTP Error[0m [37m[49mYour request was:[0m 

[36m[49mPOST https://api.twilio.com/2010-04-01/Accounts/{{ AC142b2a041b9695a2e7d73572cd7989b0 }}/Messages.json[0m 

[37m[49mTwilio returned the following information:[0m 

[34m[49m[0m 

>>> 

代碼:

from twilio.rest import TwilioRestClient 

account_sid = "{{ authorization sid copied correctly from the site }}" # Your Account SID from www.twilio.com/console 
auth_token = "{{ authorization token copied correctly from the site }}" # Your Auth Token from www.twilio.com/console 

client = TwilioRestClient(account_sid, auth_token) 

message = client.messages.create(body="Hey this is a Test SMS", 
    to="+919551771607", # Replace with your phone number 
    from_="+14066234282") # Replace with your Twilio number 

print(message.sid) 

在這個問題上有什麼任何指針可能?

+0

請張貼導致此錯誤的代碼。根據文檔,引發的異常是'TwilioRestException',它是「來自Twilio API的通用400或500級異常」。所以看起來你提出了一個不好的要求。 –

+0

我已添加代碼 –

+2

您是否在帳戶sid和令牌中包含「{{}}」? – kulkarniankita

回答

0

哦,我忘了在定義account_sid和auth_token變量時刪除大括號。刪除大括號後,它工作正常。

更正代碼:

from twilio.rest import TwilioRestClient 

    account_sid = " authorization sid copied correctly from the site " # Your Account SID from www.twilio.com/console 
    auth_token = " authorization token copied correctly from the site " # Your Auth Token from www.twilio.com/console 

    client = TwilioRestClient(account_sid, auth_token) 

    message = client.messages.create(body="Hey this is a Test SMS", 
     to="+919551771607", # Replace with your phone number 
     from_="+14066234282") # Replace with your Twilio number 

    print(message.sid)