【问题标题】:GMAIL API: <HttpError 400 when requesting https://www.googleapis.com/gmail/v1/users/myemail%40gmail.com/labels?alt=json returned "Bad Request">GMAIL API:<HttpError 400 在请求 https://www.googleapis.com/gmail/v1/users/myemail%40gmail.com/labels?alt=json 时返回“错误请求”>
【发布时间】:2017-03-23 12:49:03
【问题描述】:

我正在尝试编写一个 python 程序,该程序将使用帐户服务通过 gmail API 访问我的 gmail 帐户。我按照使用 OAuth 2.0 for Server to Server Applications

中的所有步骤进行操作

这是我的代码:

from __future__ import print_function
import httplib2
import os

from apiclient.discovery import build
from httplib2 import Http
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
from oauth2client.service_account import ServiceAccountCredentials

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[ tools.argparser]).parse_args()
except ImportError:
   flags = None

def main():
   SCOPES = ['https://www.googleapis.com/auth/gmail.modify']
   APPLICATION_NAME = 'Gmail Application'
   credentials =    ServiceAccountCredentials.from_json_keyfile_name(
        'MyKeyFile.json', scopes=SCOPES)

   http_auth = credentials.authorize(Http())
   service = build('gmail', 'v1', http=http_auth)
   results = service.users().labels().list(userId='myemail@gmail.com').execute()
   labels = results.get('labels', [])

   if not labels:
      print('No labels found.')
   else:
      print('Labels:')
      for label in labels:
        print(label['name'])


if __name__ == '__main__':
main()

当我运行它时,我收到一个错误 400 Bad Request。

任何人都遇到过这个问题。几天来我一直在改变一些事情,但没有任何效果:我使用服务 ID 而不是我的电子邮件,我使用 p12 凭据 api,使用新密钥创建新项目和新帐户服务......你可以命名它。我正在撞墙。任何人都可以帮忙吗?

谢谢

【问题讨论】:

  • 为了调试,您是否先尝试了真实的 Gmail 帐户而不是服务帐户?如果可行,请尝试使用 Gmail 帐户执行请求。如果没有,那么这对于服务帐户是不可行的。不确定这是否与此 SO thread 有关。
  • 服务帐户不能与 Gmal 一起使用,除非这是一个管理员目录电子邮件、域帐户、谷歌工作帐户,在这种情况下,您必须确保服务帐户可以访问用户 gmail .服务帐户无法与普通用户的 gmail 帐户一起使用。
  • @noogui 真正的 Gmail 帐户是什么意思。澄清一下,我正在使用我自己的个人 Gmail 帐户对此进行测试。我以前使用过帐户服务来做同样的事情,它已经工作了 2 年,突然间它就停止了,所以我不确定谷歌是否改变了他们的 API 或 oAuth2 协议中的一些基本内容。我的原始代码是用 java 编写的,这是唯一的区别。
  • DalmTo 说了什么..
  • 有没有其他方法可以通过 API 直接访问个人 gmail 帐户而无需人工输入?

标签: python gmail-api


【解决方案1】:

您应该在您的 API 管理器菜单here 的仪表板上启用您的 API

然后,选择你对 API 感兴趣的

最后,你会看到类似this

【讨论】:

    猜你喜欢
    • 2013-04-25
    • 2020-10-26
    • 2012-10-26
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多