使用 pyJWT 和 Python 解码 Apple 的 id_token (Signin)

问题描述

我如何在 Python 中解码 Apple 在注册过程中发送的 id_token?

我已经尝试过(从这里https://stackoverflow.com/a/65909432/984003

import jwt
decoded = jwt.decode(token,options={"verify_signature": False})

我收到一个错误

jwt.exceptions.InvalidAudienceError: Invalid audience

如果我将 id_token 复制粘贴到 jwt.io 页面 https://jwt.io/ 中,那么它会正确地将其解码为所有部分(标头、带有 aud、sub 的有效负载等)所以令牌本身是正确的,我拥有我需要的所有信息。

解决方法

当我提供预期的 aud 值时它会起作用。 aud 与您在首次调用 Apple 进行登录时提供的 clientId (https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_js/configuring_your_webpage_for_sign_in_with_apple) 相同,这与您在 Apple 控制台中设置的 Identifier 相同。

必须有一种方法可以在不提供 aud 的情况下做到这一点,因为这个 wbeage https://jwt.io/ 可以做到。但是,也许不是在 Python 中...

import jwt
decoded = jwt.decode(token,audience="<your app's>",options={"verify_signature": False})