I did everything like in this tutorial : https://developer.apple.com/documentation/apple_search_ads/implementing_oauth_for_the_apple_search_ads_api But it does not work, when I generate a client secret and use it in curl to request an access token like this :
curl -X POST -H 'Host: appleid.apple.com' -H 'Content-Type: application/x-www-form-urlencoded' 'https://appleid.apple.com/auth/oauth2/token?grant_type=client_credentials&client_id=SEARCHADS.XXXXXXX&client_secret=XXXXXXXXXX&scope=searchadsorg'
I got {"error":"invalid_client"}
the createclientsecret is like this:
import jwt
import datetime as dt
client_id = "SEARCHADS.XXXXXXXXXX"
team_id = "SEARCHADS.XXXXXXXXXX"
key_id = "XXXXXXXXXX"
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 = "privateKey/private-key.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"))
Please help me I stuck since 3 days