【问题标题】:gmail api gives failedPrecondition errorgmail api 给出 failedPrecondition 错误
【发布时间】:2015-03-29 00:13:25
【问题描述】:

我无法让 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"
 }
}

谢谢。

【问题讨论】:

  • 该帐户是否真的启用了邮件?例如,如果他们使用他们的 yahoo 地址注册了一个 Google 帐户(并且没有创建一个 gmail 帐户),或者它是一个未启用邮件服务的 Google Apps for Work 用户,那么这可能会导致该错误。简单的测试方法是让帐户在浏览器中登录mail.google.com
  • 是的。我正在用我自己的帐户对其进行测试。我也通过谷歌的 api explorer 尝试过,它在那里工作。在上面的代码示例中,client_email 是开发控制台页面 API 部分下列出的电子邮件地址。

标签: python gmail-api oauth2client


【解决方案1】:

试试:

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

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

【讨论】:

  • 谢谢。这有帮助。我之前尝试过,但我没有更新我的安全设置以允许客户访问该帐户。该错误消息非常具有误导性,因为我没有收到身份验证错误。
猜你喜欢
  • 2020-08-19
  • 1970-01-01
  • 2017-07-23
  • 2020-12-13
  • 2016-10-20
  • 2018-06-27
  • 2018-08-13
  • 2016-12-27
  • 2015-08-28
相关资源
最近更新 更多