Tuesday, 24 May 2022

Can't verify JWT

I am trying to create a JWT token for apple search ads like in this example : https://developer.apple.com/documentation/apple_search_ads/implementing_oauth_for_the_apple_search_ads_api

I did like this :

import jwt
import datetime as dt

client_id = "SEARCHADS.XXXXXXXXXXXXXXXXXXXXXXX"
team_id = "SEARCHADS.XXXXXXXXXXXXXXXXXXXXXXX"
key_id = "XXXXXXXXXXXXXXXXXXXXXXX"
audience = "https://appleid.apple.com"
alg = "ES256"

# Define issue timestamp.
issued_at_timestamp = int(dt.datetime.utcnow().timestamp())
# Define expiration timestamp. May not exceed 180 days from issue timestamp.
expiration_timestamp = issued_at_timestamp + 86400 * 180

# Define JWT headers.
headers = dict()
headers["alg"] = alg
headers["kid"] = key_id

# Define JWT payload.
payload = dict()
payload["sub"] = client_id
payload["aud"] = audience
payload["iat"] = issued_at_timestamp
payload["exp"] = expiration_timestamp
payload["iss"] = team_id

# Path to signed private key.
KEY_FILE = "SearchAds_PrivateKey.pem"

with open(KEY_FILE, "r") as key_file:
    key = "".join(key_file.readlines())

client_secret = jwt.encode(payload=payload, headers=headers, algorithm=alg, key=key)

with open("client_secret.txt", "w") as output:
    output.write(client_secret.decode("utf-8"))

SearchAds_PrivateKey.pem is like this:

-----BEGIN EC PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END EC PRIVATE KEY-----

The token generated has an invalid signature on jwt.io.

I found this topic: KJUR jws jsrsasign: Cannot validate ES256 token on JWT.io and I tried the solution but it does not work for me



from Can't verify JWT

No comments:

Post a Comment