1

我正在尝试使用服务帐户凭证通过 Gmail API(通过 AWS lambda)访问我的电子邮件。我没有 GSuite 帐户,因此我无法委派域范围的权限并模拟我的 user@gmail.com 帐户。

我在没有 OAuth 的情况下使用服务帐户授权,直接使用签名的 JWT 作为不记名令牌。[见]:https ://developers.google.com/identity/protocols/OAuth2ServiceAccount#jwt-auth

我还启用了对应用程序的所有 Gmail 访问权限,并使服务帐户成为控制台中的所有者角色。

我正在使用google.auth.jwt模块,但很难完全理解文档:[参见文档]:https ://google-auth.readthedocs.io/en/latest/reference/google.auth.jwt.html#module -google.auth.jwt

这是我尝试过的:

from googleapiclient.discovery import build
from httplib2 import Http
from google.auth import crypt
from google.auth import jwt

audience = 'https://mail.google.com/' ## full access scope
credentials = jwt.Credentials.from_service_account_file(
    'credentials.json', audience=audience) # json credentials from api console - for service account
service = build('gmail', 'v1', credentials=credentials)

这给了我一个 401 错误:

googleapiclient.errors.HttpError: <HttpError 401 when requesting https://www.googleapis.com/gmail/v1/users/me/messages?alt=json returned "Invalid Credentials">

credentials对象返回似乎是有效的,具有所有必要的元素,但是,我不知道如何让它“冒充”我的个人电子邮件。

4

0 回答 0