【问题标题】:Working with Gmail API from Google AppEngine使用来自 Google AppEngine 的 Gmail API
【发布时间】:2018-07-03 17:17:07
【问题描述】:

在 GAE 标准环境中,我正在努力为使用 google-api-python-client 的 Pub/Sub 推送通知注册针对 Gmail API 的 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,我打算查看的电子邮件收件箱是我 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 访问级别,因为我打算阅读、编写和发送电子邮件和草稿。

我的代码或身份验证和授权步骤中是否遗漏了什么?

【问题讨论】:

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


    【解决方案1】:

    问题解决了。我错过了模拟我域中的用户以读取他/她的邮箱的代码部分(如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()
    

    【讨论】:

      猜你喜欢
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 2013-06-25
      • 2013-07-12
      • 2016-05-19
      • 2015-06-24
      • 2015-04-16
      • 1970-01-01
      相关资源
      最近更新 更多