我正在尝试编写一个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,用新密钥创建新项目和新帐户服务......你说出来。我撞墙了。有人可以帮忙吗?
由于