【问题标题】:twilio 5.7.0 send message sample give exceptiontwilio 5.7.0 发送消息示例给出异常
【发布时间】:2018-02-19 10:47:44
【问题描述】:

我使用的是 Windows 10,我想使用 twilio API,所以首先通过终端安装了 twilio 并且安装成功,然后检查一切是否正常,我运行此代码:

import twilio
print twilio.__version__

输出是: 5.7.0

现在我想向我的手机发送消息,我注册并获取我的 account_sid、auth_token、twilio 号码并在我想向其发送消息的手机号码中注册,我在替换我的 account_sid 后尝试运行此代码,auth_token , twilio 号码并在我的电话号码中注册但它给出了例外这是代码:

from twilio.rest import TwilioRestClient
import twilio
print twilio.__version__
# Your Account SID from twilio.com/console
account_sid = "AC6791725bc93116fbd42b4cd477996f7d"
# Your Auth Token from twilio.com/console
auth_token  = "30ed212f6a2bbc05d103b67426b8509a"

client = TwilioRestClient(account_sid, auth_token)

message = client.sms.messages.create(
    body="Sent from your Twilio trial account - hello from python",
    to="my number phone goes here",
    from_="+14695356845" )

print message.sid

运行时错误:

Traceback (most recent call last):
  File "C:/Users/Black_Swan/PycharmProjects/omac1/send message.py", line 14, in <module>
    from_="+14695356845" )
  File "C:\Python27\lib\site-packages\twilio\rest\resources\sms_messages.py", line 167, in create
    return self.create_instance(kwargs)
  File "C:\Python27\lib\site-packages\twilio\rest\resources\base.py", line 365, in create_instance
    data=transform_params(body))
  File "C:\Python27\lib\site-packages\twilio\rest\resources\base.py", line 200, in request
    resp = make_twilio_request(method, uri, auth=self.auth, **kwargs)
  File "C:\Python27\lib\site-packages\twilio\rest\resources\base.py", line 164, in make_twilio_request
    uri=resp.url, msg=message, code=code)
twilio.rest.exceptions.TwilioRestException: HTTP 451 error: IP Address: 5.0.217.104

任何帮助我做错了什么?

【问题讨论】:

    标签: python twilio


    【解决方案1】:

    首先,我鼓励你使用 Python 3 而不是 Python 2,你会成为一个更好的人。

    关于错误,它返回一个 451 http 错误,在 Google 中快速搜索,我得到了这个:HTTP 451 Unavailable For Legal Reasons

    我将为您提供可能的解决方案:

    1. 检查您的手机是否可以发送消息:转到仪表板 -> 电话号码 - 管理号码 -> 单击您的 Twilio 号码。如果您可以发送短信,您可以在底部的“消息”中看到。我不能,我有一个“此电话号码无法发送消息”。消息。

    2. 检查您所在的国家/地区是否允许发送短信:SMS Geographic Permissions 链接。

    3. 另一个选项(很可能)是 Twilio 出于法律原因无法将 SMS 发送到您所在的国家/地区。 Here您可以检查从您的 Twilio 手机向您的手机国家/地区发送消息是否有任何问题。

    希望对你有帮助

    【讨论】:

    • 这是一个很好的答案!谢谢 Daniel(我在 Twilio 工作)。我要添加另一件事,调用 client.sms.messages.create 使用旧的和已弃用的 API。我会更新到client.messages.create,你可能会得到更好的结果。
    • @philnash 在消除所有提到的问题后,在您的文档上尝试完全相同的 CURL 代码仍然得到 451!
    • @Suge 您是否尝试了上述答案中的内容? 451 错误可能不是代码问题,而是与您的帐户有关,因此在您修复该问题之前,运行文档中的代码可能无法正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多