Arbaaz Ahmed
Arbaaz Ahmed

Reputation: 29

Credentials are required to create a TwilioClient

I want to send whatsapp messages using python using the Twilio module. I got a code from youtube and when ran the code ,it came with an error as

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

Here is my code:

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)

Upvotes: 1

Views: 7373

Answers (1)

Captain Jack Sparrow
Captain Jack Sparrow

Reputation: 1111

From the docs, a Client should be declared as follows:

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)

You were missing your account_sid and auth_token when you declared your Client.
As @Alan pointed out, if account_sid and auth_token are not declared in the Client, Twilio looks for them as environment variables, TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN respectively.

Upvotes: 2

Related Questions