Apple登录Python

2024-10-16 17:25:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用social auth python制作一个apple登录。现在我有一段代码在下面。当我做请求时,我得到一个无效的授权错误。我认为“id_令牌”无效。但我不知道如何获取id_令牌,因为我在请求之后获取id_令牌。有人能帮我吗。非常感谢

headers = {
   'alg': 'ES256',
   'kid': settings.SOCIAL_AUTH_APPLE_ID_KEY
}

payload = {
   'iss': settings.SOCIAL_AUTH_APPLE_ID_TEAM,
   'iat': now,
   'exp': now + TOKEN_TTL_SEC,
   'aud': 'https://appleid.apple.com',
   'sub': settings.SOCIAL_AUTH_APPLE_ID_CLIENT,
}

client_secret = jwt.encode(
   payload, 
   settings.SOCIAL_AUTH_APPLE_ID_SECRET, 
   algorithm='ES256', 
   headers=headers
 ).decode("utf-8")


headers = {'content-type': "application/x-www-form-urlencoded"}
data = {
    'client_id': 'com.scread.app',
    'client_secret': client_secret,
    'code': 'id_token',
    'grant_type': 'authorization_code'
}

res = requests.post('https://appleid.apple.com/auth/token', data=data, headers=headers)

Tags: comclientauthidappledatasecretsettings