gpt4 book ai didi

google-api - HttpError 400 Precondition check failed during users().messages().list Gmail API call using service account

转载 作者:行者123 更新时间:2023-12-03 23:45:53 29 4
gpt4 key购买 nike

我正在使用服务帐户在我的 gmail 框中列出邮件,但是在调用此 API 时出现以下错误 service.users().messages().list(userId='me').execute()

    HttpError                                 Traceback (most recent call last)
<ipython-input-44-da1dc3cccc81> in <module>
27
28 apiservice = build('gmail', 'v1', credentials=service_cred)
---> 29 response = apiservice.users().messages().list(userId='me').execute()

~/opt/anaconda3/lib/python3.7/site-packages/googleapiclient/_helpers.py in positional_wrapper(*args, **kwargs)
128 elif positional_parameters_enforcement == POSITIONAL_WARNING:
129 logger.warning(message)
--> 130 return wrapped(*args, **kwargs)
131 return positional_wrapper
132

~/opt/anaconda3/lib/python3.7/site-packages/googleapiclient/http.py in execute(self, http, num_retries)
849 callback(resp)
850 if resp.status >= 300:
--> 851 raise HttpError(resp, content, uri=self.uri)
852 return self.postproc(resp, content)
853

HttpError: <HttpError 400 when requesting https://www.googleapis.com/gmail/v1/users/me/messages?alt=json returned "Precondition check failed.">
代码:
scopes = [
"https://mail.google.com/",
"https://www.googleapis.com/auth/gmail.modify",
"https://www.googleapis.com/auth/gmail.readonly",
"https://www.googleapis.com/auth/gmail.labels",
"https://www.googleapis.com/auth/gmail.metadata",
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
]

service_cred = service_account.Credentials.from_service_account_info(
credentials, scopes=scopes)

service = build('gmail', 'v1', credentials=service_cred)
response = service.users().messages().list(userId='me').execute()

最佳答案

当我尝试从我的个人电子邮件中列出电子邮件时,我也遇到了这个问题。启用 Gmail API 时,Google 仪表板为我设置了服务帐户凭据,因此我认为这就是我应该使用的。
为了消除 400 错误,我必须添加:

delegated_credentials = credentials.with_subject('my-email@gmail.com')
https://developers.google.com/identity/protocols/oauth2/service-account#authorizingrequests .

但是,我随后收到一个错误消息,即我的服务帐户无权代表我的个人电子邮件发出请求。如果您不使用 G Suite,我认为没有办法代表用户使用服务帐户。
所以我的问题的最终解决方案是使用 OAuth 身份验证,如 Python Quickstart .

关于google-api - HttpError 400 Precondition check failed during users().messages().list Gmail API call using service account,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62846906/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com