Skip to content

Commit 12ade66

Browse files
Adding current progress on mini capstone. Completed generating the web token and working on performing requests on the WeatherKit API now.
1 parent 05cbe68 commit 12ade66

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

code/zach/mini-capstone.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from authlib.jose import jwt # https://docs.authlib.org/en/latest/jose/index.html
2+
import time # https://www.systutorials.com/how-to-get-the-epoch-timestamp-in-python/
3+
import requests
4+
5+
6+
def create_key(signature_alg, key_id, team_id, service_id, iat, key_path):
7+
"""This function takes the required information as arguments and returns an encoded key to pass as a parameter for the Apple WeatherKit API."""
8+
with open(key_path, 'rb') as f:
9+
key = f.read()
10+
11+
header = {
12+
"alg": signature_alg,
13+
"kid": key_id,
14+
"id": f"{team_id}.{service_id}",
15+
}
16+
17+
payload = {
18+
"iss": team_id,
19+
"iat": iat,
20+
"exp": iat + 100,
21+
"sub": service_id
22+
}
23+
24+
s = jwt.encode(header, payload, key)
25+
26+
return s
27+
28+
def get_weather(language='en', latitude=0, longitude=0, web_token):
29+
response = requests.get(f"https://weatherkit.apple.com/api/v1/weather/{language}/{latitude}/{longitude}", params={
30+
'Authorization': f'Bearer {web_token}'
31+
})
32+
print(response.json())
33+
34+
def main():
35+
web_token = create_key("ES256",
36+
"74B4NT7KNA",
37+
"AJAJ8VTADD",
38+
"com.homedashboard.test",
39+
int(time.time()),
40+
"C:/Users/zacha/OneDrive/Coding/Keys/Home_Dashboard/WeatherKit/AuthKey_74B4NT7KNA.p8"
41+
)
42+
43+
44+
45+
46+
main()

0 commit comments

Comments
 (0)