Alex Kopels
Alex Kopels

Reputation: 21

I am unable to send an sms through Twilio tutorial code because of unknown error

So I'm using the sample code provided by Twilio to send an SMS to my phone. When using the sample code I keep getting the same error and I'm not sure what the error is. I have already tried searching through this website to see if anyone has had any similar errors I also followed the Twilio step by step process for setting up and receiving the SMS. So I am very confused as to why this is happening. None of the resources I used gave me an answer as to why this is happening. My phone number is starred out for safety reasons.

# 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)

Error below:

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'

Upvotes: 2

Views: 577

Answers (2)

CryptoFool
CryptoFool

Reputation: 23089

You are mixing up two concepts here:

  1. Hard coding your Sid and Auth Token values in your code
  2. Pulling your Sid and Auth Token vaules from the execution environment.

You only want to do one of these or the other, not both. The most straightforward approach would be to do just #1, which would look like this:

account_sid = 'ACa30b8fa59e7eba0e6dc5dbedaf5e8fcb'
auth_token = 'REDACTED'

If you want to do #2, then you need to define two exported variables in the environment in which you are running your program. It looks like you're on a Mac, so you could do something like this in, say, your ~/.bash_profile file:

export TWILIO_SID=ACa30b8fa59e7eba0e6dc5dbedaf5e8fcb
export TWILIO_AUTH_TOKEN=REDACTED

and then do this in your Python code:

account_sid = os.environ['TWILIO_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']

UPDATE: I'll add that #2 is much preferred in production code because it avoids you having to check in sensitive account credentials to a shared source code repository that other people will see. That's the whole reason for the added complexity. If you're just fooling around or doing a personal project, then #1 might be fine. If you're doing work where your code will be seen by others, do #2 to keep your credentials to yourself.

Upvotes: 3

Sxribe
Sxribe

Reputation: 829

Read your error!

Your call to os.eviron[your-key] is erroring with raise KeyError(key) from None KeyError. When googled, this error occurs when you are trying to access a missing key from the dictionary in os.eviron. Did you set your keys in env variables? If the key you are indexing in your code is your account_sid and auth_token, then you are doing it wrong for sure

Upvotes: 0

Related Questions