我正在使用 python google api 客户端库来处理使用 google 的 platform.js 库从前端获取的 token_type:"Bearer"
的 id_token
。
通过调用即可轻松完成 token 的验证
google.oauth2.id_token.verify_oauth2_token(
request.POST['bearer_token'],
google.auth.transport.requests.Request(),
client_id )
现在我想要访问用户的 Google 云端硬盘(完全访问权限)。到目前为止我所理解的是,要构建服务对象,我们需要像这样的凭据对象。
drive_service = build('drive', 'v3', credentials=credentials)
此时我所拥有的只是一个不记名 token 。如何将其转换为凭据以便访问驱动器 API?
最佳答案
凭据对象来自“google.oauth2.credentials”,要设置您的凭据对象,您需要知道某些值:
- token
- 刷新 token
- token URI
- 客户端 ID
- 客户端 secret
以下是我如何访问用户的驱动器:
credentials = Credentials(
token=token,
refresh_token=refresh_token,
token_uri="https://www.googleapis.com/oauth2/v3/token",
client_id=client_id,
client_secret=client_secret,
)
drive_service = build('drive', "v3", credentials=credentials)
您可以尝试将“Bearer Token”设置为“Token”,但我不确定它是否有效。这是“Credential”对象的文档 https://google-auth.readthedocs.io/en/latest/reference/google.oauth2.credentials.html
关于python - 获取 Google OAuth2 凭据或从承载类型 token 刷新 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54347813/