TIL Python Basics Day 35 - Keys, Authentication & Environment Variables: Send SMS using Twilio
API Keys, Authentication
Task1. Weather info
List slicing to fetch only 0-11 hours data from hourly tab. Read documentation for appropriate parameters.
import requests
api_key = "bdb50b72050f8f7f1a932c9e1176fd60"
lat = 37.566536
lon = 126.977966
exclude = "current,minutely,daily,alerts"
parameters = {
"lat": lat,
"lon": lon,
"exclude": exclude,
"appid": api_key
}
response = requests.get(url="https://api.openweathermap.org/data/2.5/onecall?", params=parameters)
response.raise_for_status()
data = response.json()
hourly = data["hourly"][0:12] #slicing
will_rain = False
for item in hourly:
condition_code = int(item["weather"][0]["id"])
if condition_code < 700:
will_rain = True
if will_rain:
print("Bring an umbrella.")
else:
print("You don't need an umbrella.")
Project: Sending SMS via the Twilio API
import requests
from twilio.rest import Client
account_sid = 'AC'
auth_token = '1af'
api_key = "bdb50b72050f8f7f1a932c9e1176fd60"
lat = 52.205338
lon = 0.121817
exclude = "current,minutely,daily,alerts"
parameters = {
"lat": lat,
"lon": lon,
"exclude": exclude,
"appid": api_key
}
response = requests.get(url="https://api.openweathermap.org/data/2.5/onecall?", params=parameters)
response.raise_for_status()
data = response.json()
hourly = data["hourly"][0:12] #slicing
will_rain = False
for item in hourly:
condition_code = int(item["weather"][0]["id"])
if condition_code < 700:
will_rain = True
if will_rain:
client = Client(account_sid, auth_token)
message = client.messages \
.create(
body="It's going to rain today. Remember to bring an umbrella!",
from_='+12082097605',
to='+8210-'
)
print(message.status)
Environment Variables
why? 1. Convenience: don't wanna mess around code base when having complex project. 2. Security: might be uploading code base somewhere. Authentication keys stored in the same code isn't secure.
Be aware of api keys being stollen when uploading your own code to github and bitbucket.
Always strip API keys and use environment variables. we can set environment variables. -> step by step instructions at lecture 315
fun APIs
Words
tap into
"get some benefit"
If only we could tap into all that energy and creativity.
Reference
この問題について(TIL Python Basics Day 35 - Keys, Authentication & Environment Variables: Send SMS using Twilio), 我々は、より多くの情報をここで見つけました https://velog.io/@daylee/TIL-Python-Basics-Day-35-Keys-Authentication-Environment-Variables-Send-SMSテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol