使用Google AppEngine中的Gmail API

时间:2018-01-24 15:38:17

标签: python google-app-engine gmail-api

在GAE标准环境中,我很难使用watch()在Pub / Sub推送通知中针对Gmail API注册google-api-python-client来电。

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

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. 我已经创建了一个发布/订阅主题,这是我传递给watch()请求的主题
  2. 我使用G-Suite,我打算观看的电子邮件收件箱是我G-Suite商业域的一部分。
  3. 对于此任务,我使用已启用G-Suite域范围委派的服务帐户。我已经下载了我提供的.json服务帐户文件,以便获取oauth2client.service_account.Credentials对象(我看到在日志中成功交换的访问和刷新令牌)。 json服务文件与我的main.py脚本(我的项目的根目录)放在同一个文件夹中。
  4. 在我的G-Suite管理面板中,我已启用从2开始的api访问服务帐户,范围为https://www.googleapis.com/auth/gmail.modify。我正在使用gmail.modify访问级别,因为我打算阅读,编写和发送电子邮件和草稿。
  5. 我的代码或身份验证和授权步骤中是否遗漏了某些内容?

1 个答案:

答案 0 :(得分:0)

问题解决了。我错过了从我的域中冒充用户以便阅读他/她的邮箱的代码部分(如here所述)。

更正后的代码如下所示:

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()