我正在使用谷歌服务帐户从没有 UI 界面的 gmail 帐户访问所有邮件但是当我执行我的代码时它给了我错误
googleapiclient.errors.HttpError:https://www.googleapis.com/gmail/v1/users/me/labels?alt=json 返回“错误请求”>
但是当我从https://console.developers.google.com/apis/api/gmail.googleapis.com/quotas检查配额时 ,会显示我使用我的 python 代码所做的所有请求,但它总是在我执行时返回错误请求下面的代码。
import httplib2
from apiclient import discovery
from oauth2client.service_account import ServiceAccountCredentials
def get_credentials():
scopes = ['https://mail.google.com/',
'https://www.googleapis.com/auth/gmail.compose',
'https://www.googleapis.com/auth/gmail.metadata',
'https://www.googleapis.com/auth/gmail.readonly',
'https://www.googleapis.com/auth/gmail.labels',
'https://www.googleapis.com/auth/gmail.modify',
'https://www.googleapis.com/auth/gmail.metadata',
'https://www.googleapis.com/auth/gmail.settings.basic']
credentials = ServiceAccountCredentials.from_json_keyfile_name(
'client_secret.json', scopes=scopes)
return credentials
def main():
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('gmail', 'v1', http=http)
results = service.users().labels().list(userId='me').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()