仅使用auth\u令牌或refresh\u令牌通过Google Python SDK进行身份验证

2024-03-28 14:38:48 发布

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

我有一堆gmail.存储包含用户帐户访问信息的JSON blob的文件。我使用PHP获得这些凭证集,以提示用户验证/授权我的应用程序。我可以使用这些凭据调用gmailapi。JSON包含以下字段:

{"access_token": xxxxxx,
"token_type":"Bearer",
"expires_in":3599,
"refresh_token":"xxxxxx",
"created":1421545394}

但是,当我使用Python SDK for Gmail使用以下凭据文件对调用进行身份验证时:

 credentials = storage.get()

它说它需要_module_class和其他一些字段。当我使用Python获取信息时,我能够获得这些信息,但不能使用PHP。你知道如何使用上面的代码在没有这些无关字段的情况下获取凭据吗?你知道吗


Tags: 文件用户tokenjson应用程序accesstype帐户
1条回答
网友
1楼 · 发布于 2024-03-28 14:38:48

只需在再次使用creds之前调用refresh方法。你知道吗

storage = file.Storage("auth.dat")
credentials = storage.get()

http = httplib2.Http()
credentials.refresh(http)   # <     
http = credentials.authorize(http)

service = build('whatever_service', 'v2', http=http)

相关问题 更多 >