Gmail API用户模拟(Python)

Gmail API用户模拟(Python),python,google-api,Python,Google Api,我正试图通过一个服务帐户访问Gmail API,并模拟我公司的G套件域上的一个用户来更改他们的电子邮件设置。我按照谷歌文档页面上的说明进行了操作,但我根据该指南编写的代码一直给我一个HTTP 400:Bad请求错误 这是我的代码,我尝试检索标签,看看是否可以成功模拟用户: from __future__ import print_function import httplib2 import os from apiclient import discovery from oauth2clien

我正试图通过一个服务帐户访问Gmail API,并模拟我公司的G套件域上的一个用户来更改他们的电子邮件设置。我按照谷歌文档页面上的说明进行了操作,但我根据该指南编写的代码一直给我一个HTTP 400:Bad请求错误

这是我的代码,我尝试检索标签,看看是否可以成功模拟用户:

from __future__ import print_function
import httplib2
import os

from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage

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

def main():


    from oauth2client.service_account import ServiceAccountCredentials

    #scopes = ['https://mail.google.com/', 'https://www.googleapis.com/auth/gmail.labels','https://www.googleapis.com/auth/gmail.readonly','https://www.googleapis.com/auth/gmail.settings.basic ','https://www.googleapis.com/auth/gmail.settings.sharing']
    scopes = ['https://mail.google.com/']

    credentials = ServiceAccountCredentials.from_json_keyfile_name('/Users/bartoszanimucki/Downloads/service_secret.json', scopes)
    http = credentials.authorize(httplib2.Http())

    delegated_credentials = credentials.create_delegated('user@domain.com')
    http_auth = credentials.authorize(httplib2.Http())

    service = discovery.build('gmail', 'v1', http=http_auth)

    results = service.users().labels().list(userId='me').execute()

    print(results)

if __name__ == '__main__':
    main()
脚本在execute()方法失败,使用HTTP 400。有什么想法吗

作为参考,以下是我从Python中得到的错误消息:

Traceback (most recent call last):
  File "sample.py", line 37, in <module>
    main()
  File "sample.py", line 32, in main
    results = service.users().labels().list(userId='bb@comcamenergy.com').execute()
  File "/Library/Python/2.7/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/googleapiclient/http.py", line 840, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/gmail/v1/users/bb%40comcamenergy.com/labels?alt=json returned "Bad Request">
回溯(最近一次呼叫最后一次):
文件“sample.py”,第37行,在
main()
文件“sample.py”,第32行,在main中
结果=service.users().labels().list(userId=)bb@comcamenergy.com)。执行()
文件“/Library/Python/2.7/site packages/oauth2client/_helpers.py”,第133行,在位置包装中
已包装退货(*args,**kwargs)
文件“/Library/Python/2.7/site packages/googleapiclient/http.py”,第840行,在execute中
raise HttpError(resp,content,uri=self.uri)
GoogleAppClient.errors.HttpError:

我想出来了!我试图使用链接到服务帐户的
凭据
对象,而不是
委派的\u凭据
。它通过更改行来修复:

http_auth = credentials.authorize(httplib2.Http())


谢谢你的提示!注意细节很重要。

只是确认一下。您是否从仪表板启用了google api?是的,admin.google.com上的“启用api访问”复选框已选中。在域上使用我的非服务凭据有效,因此API已打开。我需要是您尝试访问的用户。服务帐户没有gmail帐户,因此我无法工作。我将“我”更改为我尝试访问的同一封电子邮件。仍然有相同的错误。如果您添加实际的错误消息将有所帮助。我猜是这样的:“客户端未经授权使用此方法检索访问令牌。”,在以前的api调用中,我也突然(过去两天)想到了这一点。
http_auth = delegated_credentials.authorize(httplib2.Http())