Working with Gmail API from Google AppEngine



在 GAE 标准环境中,我正在努力使用google-api-python-client注册针对 Gmail API 的 Pub/Sub 推送通知的watch()调用。

以下是我的代码的相关摘录:

import googleapiclient.discovery
from oauth2client import service_account
SCOPES = ['https://www.googleapis.com/auth/gmail.modify']
SERVICE_ACCOUNT_FILE = '<My-project>-<short-ID>.json'
credentials = service_account.ServiceAccountCredentials.from_json_keyfile_name(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
gmail = googleapiclient.discovery.build('gmail', 'v1', credentials=credentials)
watchRequest = {
'labelIds' : ['INBOX'],
'topicName' : 'projects/<my-project>/topics/<my-topic>'
}
gmail.users().watch(userId='<email-I-need-to-watch>', body=watchRequest).execute()

在触发这部分代码后,我得到:

Traceback (most recent call last):
File     "/base/alloc/tmpfs/dynamic_runtimes/python27/54c5883f70296ec8_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File     "/base/alloc/tmpfs/dynamic_runtimes/python27/54c5883f70296ec8_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in     _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/base/alloc/tmpfs/dynamic_runtimes/python27/54c5883f70296ec8_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/base/data/home/apps/e~<my-project>-191008/20180124t154459.407164278206739455/main.py", line 68, in <module>
gmail.users().watch(userId='<email-I-need-to-watch>', body=watchRequest).execute()
File "/base/data/home/apps/e~<my-project>/20180124t154459.407164278206739455/lib/oauth2client/_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "/base/data/home/apps/e~<my-project>/20180124t154459.407164278206739455/lib/googleapiclient/http.py", line 844, in execute
raise HttpError(resp, content, uri=self.uri)
HttpError: <HttpError 400 when requesting https://www.googleapis.com/gmail/v1/users/<email-I-need-to-watch>/watch?alt=json returned "Bad Request">

关于身份验证和授权,以下是我到目前为止所做的:

  1. 我创建了一个 Pub/Sub 主题,这是我传递给watch()请求的主题
  2. 我使用 G-Suite
  3. ,我打算观看的电子邮件收件箱是我的 G-Suite 业务域的一部分。
  4. 对于此任务,我使用启用了 G-Suite 全网域委派的服务帐号。我已经下载了我提供的 .json 服务帐户文件,以便获取oauth2client.service_account.Credentials对象(我看到访问和刷新令牌在日志中成功交换)。json 服务文件与我的main.py脚本(我的项目的根目录)位于同一文件夹中。
  5. 在我的 G-Suite 管理面板中,我启用了从 2. 的范围为https://www.googleapis.com/auth/gmail.modify的服务帐户的 api 访问权限。我正在使用gmail.modify访问级别,因为我打算阅读、编写和发送电子邮件和草稿。

我的代码或身份验证和授权步骤中是否遗漏了某些内容?

问题解决了。我缺少代码中用于模拟我的域中的用户以读取他/她的邮箱的部分(如此处所述)。

更正后的代码如下所示:

import googleapiclient.discovery
from google.oauth2 import service_account
SCOPES = ['https://www.googleapis.com/auth/gmail.modify']
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES
)
credentials = credentials.with_subject('<email-I-need-to-watch>')
gmail = googleapiclient.discovery.build('gmail', 'v1', credentials=credentials)
watchRequest = {
'labelIds' : ['INBOX'],
'topicName' : 'projects/<my-project>/topics/<my-topic>'
}
gmail.users().watch(userId='me', body=watchRequest).execute()

最新更新