bluethundr
bluethundr

Reputation: 1325

Send Text Message via Twilio in Python

I'm following this tutorial: How to Send an SMS With Python Using Twilio.

I've setup these variables as environment variables on my windows machine:

CELL_PHONE_NUMBER = +12015555555
TWILIO_ACCOUNT_SID = my_twilio_account_sid
TWILIO_AUTH_TOKEN = my_twilio_auth_token
TWILIO_PHONE_NUMBER = +12016666666

This is my code:

import os
from twilio.rest import Client


account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
auth_token = os.environ.get('TWILIO_AUTH_TOKEN')

client = Client(account_sid, auth_token)

client.messages.create(from_=os.environ.get('TWILIO_PHONE_NUMBER'),
                       to=os.environ.get('CELL_PHONE_NUMBER'),
                       body='You just sent an SMS from Python using Twilio!')

But I'm getting this error:

Traceback (most recent call last):
  File ".\sms.py", line 10, in <module>
    client.messages.create(from_=os.environ.get('TWILIO_PHONE_NUMBER'),
  File "C:\Users\tdun0002\AppData\Local\Programs\Python\Python38-32\lib\site-packages\twilio\rest\api\v2010\account\message\__init__.py", line 86, in create
    payload = self._version.create(method='POST', uri=self._uri, data=data, )
  File "C:\Users\tdun0002\AppData\Local\Programs\Python\Python38-32\lib\site-packages\twilio\base\version.py", line 209, in create
    raise self.exception(method, uri, response, 'Unable to create record')
twilio.base.exceptions.TwilioRestException:
[31m[49mHTTP Error[0m [37m[49mYour request was:[0m

[36m[49mPOST /Accounts/PN7afb27dc8e041f18dcf91fefe306ad34/Messages.json[0m

[37m[49mTwilio returned the following information:[0m

[34m[49mUnable to create record: Authentication Error - invalid username[0m

[37m[49mMore information may be available here:[0m

[34m[49mhttps://www.twilio.com/docs/errors/20003[0m

What am I doing wrong?

Upvotes: 1

Views: 749

Answers (1)

Kasem Alsharaa
Kasem Alsharaa

Reputation: 920

According to Twilio docs

ERROR - 20003
Permission Denied
You lack permission to the resource and method you requested.

Troubleshooting steps

Verify the Account SID and Auth Token are correct
Verify the correct Account is being accessed
Ensure the Account is active, not suspended or closed
Ensure no extra characters or spaces are being included
Ensure JWT is only used with API Key

Try printing your account_sid and auth_token to double check they're correct, and verify that they match the ones on the twilio console

Upvotes: 1

Related Questions