gpt4 book ai didi

python - gmail api 给出 failedPrecondition 错误

转载 作者:太空宇宙 更新时间:2023-11-03 13:41:49 24 4
gpt4 key购买 nike

我无法让 gmail api 工作。我正在使用 JWT 的服务器到服务器身份验证。

google-api-python-client==1.4.0
httplib2==0.9
oauth2client==1.4.7
pycrypto==2.6.1

我的代码是这样的。

with open(CLIENT_SECRET_FILE) as f:
data = json.loads(f.read())
private_key = data['private_key']
client_email = data['client_email']
credentials = SignedJwtAssertionCredentials(client_email, private_key, scope=OAUTH_SCOPE)

http = credentials.authorize(http)
gmail_service = build('gmail', 'v1', http=http)
try:
threads = gmail_service.users().messages().list(userId='me').execute()
except Exception as e:
print e
print e.content

响应是

<HttpError 400 when requesting https://www.googleapis.com/gmail/v1/users/me/messages?alt=json returned "Bad Request">
{
"error": {
"errors": [
{
"domain": "global",
"reason": "failedPrecondition",
"message": "Bad Request"
}
],
"code": 400,
"message": "Bad Request"
}
}

谢谢。

最佳答案

尝试:

credentials = SignedJwtAssertionCredentials(client_email,
private_key,
scope=OAUTH_SCOPE,
sub='user@yourdomain.com')

sub= 告诉服务帐户您希望模拟哪个帐户。没有它,您将作为服务帐户用户进行身份验证,正如 Eric 指出的那样,该用户没有 Gmail 邮箱。

关于python - gmail api 给出 failedPrecondition 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29307965/

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