本文介绍了gmail api给出failedPrecondition错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用gmail api.我正在通过JWT使用服务器到服务器身份验证.

I can't get gmail api to work. I'm using server-to-server authentication with 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='[email protected]')

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

The sub= tells the Service Account which account you wish to impersonate. Without it, you're authenticating as the Service account user which, as Eric pointed out, doesn't have a Gmail mailbox.

这篇关于gmail api给出failedPrecondition错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 08:41