我想使用Twilio模块使用python发送whatsapp消息。我从youtube获得了一个代码,运行该代码时,它带有一个错误,如
Traceback (most recent call last):
File "whatsapp.py", line 4, in <module>
client = Client()
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\twilio\rest\__init__.py", line
54, in __init__
raise TwilioException("Credentials are required to create a TwilioClient")
twilio.base.exceptions.TwilioException: Credentials are required to create a TwilioClient
这是我的代码:
from twilio.rest import Client
client = Client()
from_whatsapp_number = 'whatsapp: +60***86744'
to_whatsapp_number = 'whatsapp: +8134***727'
client.messages.create(body='Testing message using python',
from_ = from_whatsapp_number,
to = to_whatsapp_number)
从docs开始,Client
应该声明如下:
from twilio.rest import Client
# Your Account SID from twilio.com/console
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
# Your Auth Token from twilio.com/console
auth_token = "your_auth_token"
client = Client(account_sid, auth_token)
您在声明account_sid
时丢失了auth_token
和Client
。正如@Alan指出的那样,如果未在account_sid
中声明auth_token
和Client
,则Twilio将它们分别作为环境变量TWILIO_ACCOUNT_SID
和TWILIO_AUTH_TOKEN
查找。