创建TwilioClient所需的凭据

问题描述 投票:0回答:1

我想使用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)
python twilio message whatsapp
1个回答
0
投票

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_tokenClient。正如@Alan指出的那样,如果未在account_sid中声明auth_tokenClient,则Twilio将它们分别作为环境变量TWILIO_ACCOUNT_SIDTWILIO_AUTH_TOKEN查找。

© www.soinside.com 2019 - 2024. All rights reserved.