Weatherkit REST API is returning 401 errors {'reason': 'NOT_ENABLED'}

I created an identifier, but did not select "Sign In with Apple" I created a key, and enabled the WeatherKit service.

I have a simple python script to retrieve from the API, but I am getting "NOT ENABLED"

import datetime
import time

# pip install requests PyJWT cryptography
import jwt
import requests
import json

from cryptography.hazmat.primitives.serialization import load_ssh_private_key
from hashlib import sha1

with open("/Users/don/.ssh/AuthKey_LBV5W26ZRJ.p8", "r") as f:
    myKey = f.read()

# matches my service id
WEATHERKIT_SERVICE_ID = "net.ag6hq.sandysclock"

#This is my id, redacted here
WEATHERKIT_TEAM_ID = "<redacted>"

# this is my private key, redacted here
WEATHERKIT_KID = "<redacted>"  # key ID
WEATHERKIT_KEY = myKey

WEATHERKIT_FULL_ID = f"{WEATHERKIT_TEAM_ID}.{WEATHERKIT_SERVICE_ID}"
thisLat           = 34.03139251897727
thisLon           = -117.41704704143667

def fetch_weatherkit(
  lang="en",
  lat="34.031392",
  lon="-117.41704",
  country="US",
  timezone="US/Los_Angeles",
  datasets = "currentWeather,forecastDaily,forecastHourly,forecastNextHour",
 ):
  url = f"https://weatherkit.apple.com/api/v1/weather/{lang}/{lat}/{lon}?dataSets={datasets}&countryCode={country}&timezone={timezone}"
  now = int(time.time())
  exp = now + (3600 * 24)

  token_payload = {
    "sub": WEATHERKIT_SERVICE_ID,
    "iss": WEATHERKIT_TEAM_ID,
    "exp": exp,
    "iat": now
  }
  token_header = {
    "kid": WEATHERKIT_KID,
    "id": WEATHERKIT_FULL_ID,
    "alg": "ES256",
    "typ": "JWT"
  }

  token = jwt.encode(token_payload, WEATHERKIT_KEY, headers=token_header, algorithm="ES256")

  response = requests.get(url, headers={'Authorization': f'Bearer {token}'})
  return response
####
End of Def

myFetch=fetch_weatherkit()
myStatus=myFetch.status_code
myJSON=myFetch.json()
print("myJSON=" + str(myJSON))
print("myStatus=" + str(myStatus))

This outputs:

python weatherkit.py
myJSON={'reason': 'NOT_ENABLED'}
myStatus=401

I get the same results if I use the jwt.io service to create a token and use curl

What am I doing wrong?

Post not yet marked as solved Up vote post of ag6hq Down vote post of ag6hq
775 views
  • I put together a YouTube video describing how I overcame the JWT fiasco. Please comment if it helps:https://youtu.be/TQdtIb-Sgqk?si=j0LwEGPh4lkn_wdR

Add a Comment

Replies

I am getting the same error. Although I'm not using REST API. I'm using Swift.

Based on a few other replies on other similar messages, we are not alone.

My code was working fine last night. Tried today after making a few changes and I keep getting error 401. I reverted my new changes and still get the error.

I have up-to-date provisioning profile and certificates, and have WeatherKit correctly integrated (as of yesterday at least).

I went back to Apple's FlightPlanner project and even that doesn't work anymore. I made a very stripped down weatherkit program and can't get that to work either.

Here's my error message from FlightPlanner in case others are experiencing it too. Apple, please help.

Some of the Output:

2023-03-14 20:46:35.082748-0700 FlightPlanner[12854:203691] [WeatherDataService] Received authentication failure for request: 37F8FBEC-E6B5-4124-B28F-F556A90061CE:1

2023-03-14 20:46:35.085862-0700 FlightPlanner[12854:203691] [WeatherService] Encountered an error when fetching weather data subset; location=<+37.62130000,-122.37900000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/14/23, 8:46:33 PM Pacific Daylight Time,  error=invalidAuthorization(<NSHTTPURLResponse: 0x600003cfa7e0> { URL: https://weather-data.apple.com/v3/weather/en/37.621/-122.379?timezone=America/Los_Angeles&dataSets=currentWeather&country=US } { Status Code: 401, Headers {

    "Access-Control-Allow-Origin" =     (

        "*"

    );

    "Cache-Control" =     (

        "max-age=0, no-cache, no-store"

    );

    Connection =     (

        close

    );

    "Content-Security-Policy" =     (

        "default-src 'self';"

    );

    Date =     (

        "Wed, 15 Mar 2023 03:46:34 GMT"

    );

    Expires =     (

        "Wed, 15 Mar 2023 03:46:34 GMT"

    );

    Pragma =     (

        "no-cache"

    );

    Server =     (

        Apple

    );

    "Strict-Transport-Security" =     (

        "max-age=31536000; includeSubdomains"

    );

    "X-Cache" =     (

        "TCP_MISS from a104-123-67-38.deploy.akamaitechnologies.com (AkamaiGHost/11.0.2-47096334) (-)"

    );

    "X-Content-Type-Options" =     (

        nosniff

    );

    "X-Frame-Options" =     (

        SAMEORIGIN

    );

    "X-REQUEST-ID" =     (

        "e9e27d87-209a-4c26-9048-cb7250784b99"

    );

    "X-XSS-Protection" =     (

        "1; mode=block"

    );

} })
  • Is your app working today?

Add a Comment

I don't have a solution but also have a basic WeatherKit script (REST) that worked yesterday and is now returning NOT_ENABLED through the script and through using the same bearer token with curl.

  • How is your app working now?

Add a Comment