How can I generate an authorization code/client secret in python for apple sign in and device check?
How to generate apple authorization token/client secret?
12.2k views Asked by ARR At
2
There are 2 answers
0
On
Here is another version of the code provided by @ARR and some links:
- team_id: https://developer.apple.com/help/account/manage-your-team/locate-your-team-id/
- client_id: The app identifier it looks like "my.app.com", you can find all your identifiers here: https://developer.apple.com/account/resources/identifiers/list
- key_id: This one can be obtained after creating the private_key in the following link https://appstoreconnect.apple.com/access/api
import jwt
import time
def generate_token():
with open("file.p8", "r") as f:
private_key = f.read()
team_id = "123"
client_id = "bundle.id"
key_id = "123"
validity_minutes = 20
timestamp_now = int(time.time())
timestamp_exp = timestamp_now + (60 * validity_minutes)
# Assuming `last_token_expiration` is a class variable defined somewhere else
# cls.last_token_expiration = timestamp_exp
data = {
"iss": team_id,
"iat": timestamp_now,
"exp": timestamp_exp,
"aud": "https://appleid.apple.com",
"sub": client_id
}
token = jwt.encode(
payload=data,
key=private_key.encode('utf-8'),
algorithm="ES256",
headers={"kid": key_id}
)
print(token)
generate_token()
the complete code will look like this