【发布时间】:2021-02-09 15:04:21
【问题描述】:
所以我使用 Twilio 提供的示例代码向我的手机发送短信。使用示例代码时,我不断收到相同的错误,但我不确定错误是什么。我已经尝试通过这个网站搜索,看看是否有人有任何类似的错误我还按照 Twilio 一步一步的过程来设置和接收 SMS。所以我很困惑为什么会这样。我使用的任何资源都没有给我一个关于为什么会发生这种情况的答案。出于安全原因,我的电话号码已加注星标。
# Download the helper library from https://www.twilio.com/docs/python/install
import os
from twilio.rest import Client
# Your Account Sid and Auth Token from twilio.com/console
# and set the environment variables. See http://twil.io/secure
account_sid = os.environ['ACa30b8fa59e7eba0e6dc5dbedaf5e8fcb']
auth_token = os.environ['REDACTED']
client = Client(account_sid, auth_token)
message = client.messages \
.create(
body="What is up Med",
from_='+***********',
to='+***********'
)
print(message.sid)
以下错误:
Traceback (most recent call last):
File "/Users/administrator/Documents/Unit 5 Proj.py", line 8, in <module>
account_sid = os.environ['ACa30b8fa59e7eba0e6dc5dbedaf5e8fcb']
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/os.py", line 679, in __getitem__
raise KeyError(key) from None
KeyError: 'ACa30b8fa59e7eba0e6dc5dbedaf5e8fcb'
【问题讨论】:
-
输入 account_sid 和 auth_token 时不需要使用 os.environ,因为您对实际 id 进行了硬编码。如果您使用变量名而不是实际数字,则只需将其添加为环境变量。
-
是的,正如@SamanthaCruz 暗示的那样,您可能想要的是
account_sid= 'ACa30b8fa59e7eba0e6dc5dbedaf5e8fcb', and a similar thing forauth_token`。