Created
July 4, 2022 20:48
-
-
Save pizzapanther/8a29b5462c786f0bb94a257ea535cc0f to your computer and use it in GitHub Desktop.
WeatherKit Authorization with JWT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
# pip install requests PyJWT cryptography | |
import jwt | |
import requests | |
# https://developer.apple.com/account/resources/identifiers/list/serviceId | |
WEATHERKIT_SERVICE_ID = "" # Create service like (use same ending): com.example.weatherkit-client | |
# https://developer.apple.com/account/ - click Membership in nav to get Team ID | |
WEATHERKIT_TEAM_ID = "" | |
# https://developer.apple.com/account/resources/authkeys/list | |
WEATHERKIT_KID = "" # key ID | |
WEATHERKIT_KEY = "" # contents of p8 file | |
WEATHERKIT_FULL_ID = f"{WEATHERKIT_TEAM_ID}.{WEATHERKIT_SERVICE_ID}" | |
def fetch_weatherkit( | |
lang="en", | |
lat="29.8752", | |
lon="-98.2625", | |
country="US", | |
timezone="US/Chicago", | |
datasets = "currentWeather,forecastDaily,forecastHourly,forecastNextHour", | |
): | |
url = f"https://weatherkit.apple.com/api/v1/weather/{lang}/{lat}/{lon}?dataSets={datasets}&countryCode={country}&timezone={timezone}" | |
now = datetime.datetime.now(tz=datetime.timezone.utc) | |
exp = now + datetime.timedelta(hours=1) | |
token_body = { | |
"sub": WEATHERKIT_SERVICE_ID, | |
"iss": WEATHERKIT_TEAM_ID, | |
"exp": exp, | |
"iat": now | |
} | |
token_headers = { | |
"kid": WEATHERKIT_KID, | |
"id": WEATHERKIT_FULL_ID | |
} | |
token = jwt.encode(token_body, WEATHERKIT_KEY, headers=token_headers, algorithm="ES256") | |
response = requests.get(url, headers={'Authorization': f'Bearer {token}'}) | |
return response.json() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment