简体   繁体   English

gmail api给出了failedPrecondition错误

[英]gmail api gives failedPrecondition error

I can't get gmail api to work. 我无法让gmail api工作。 I'm using server-to-server authentication with JWT. 我正在使用JWT进行服务器到服务器身份验证。

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

My code looks like this. 我的代码看起来像这样。

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

The response is 回应是

<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"
 }
}

Thanks. 谢谢。

Try: 尝试:

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

The sub= tells the Service Account which account you wish to impersonate. sub =告诉服务帐户您要模拟哪个帐户。 Without it, you're authenticating as the Service account user which, as Eric pointed out, doesn't have a Gmail mailbox. 没有它,您将作为服务帐户用户进行身份验证,正如Eric指出的那样,该用户没有Gmail邮箱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM