Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用服务帐户从gmail获取邮件_Python_Gmail Api_Google Api Client_Service Accounts_Google Python Api - Fatal编程技术网

Python 使用服务帐户从gmail获取邮件

Python 使用服务帐户从gmail获取邮件,python,gmail-api,google-api-client,service-accounts,google-python-api,Python,Gmail Api,Google Api Client,Service Accounts,Google Python Api,我使用谷歌服务帐户访问所有来自gmail帐户的邮件,没有UI界面,但当我执行代码时,它给了我一个错误 GoogleAppClient.errors.HttpError:https://www.googleapis.com/gmail/v1/users/me/labels?alt=json 返回 “错误请求”> 但是当我从 这里显示了我使用python代码执行的所有请求,但当我执行下面的代码时,它总是返回错误的请求 import httplib2 from apiclient import dis

我使用谷歌服务帐户访问所有来自gmail帐户的邮件,没有UI界面,但当我执行代码时,它给了我一个错误

GoogleAppClient.errors.HttpError:https://www.googleapis.com/gmail/v1/users/me/labels?alt=json 返回 “错误请求”>

但是当我从 这里显示了我使用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()

自从这篇文章第一次发表以来,发生了很大的变化

如果还有其他人在寻找答案。这是我今天对Gmail API的初始化过程

from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'

def main():   
    # Setup for the Gmail API
    store = file.Storage('token.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
        creds = tools.run_flow(flow, store)
    service = build('gmail', 'v1', http=creds.authorize(Http()))

    # Call the Gmail API to fetch INBOX
    results = service.users().labels().list().execute()

一个主要区别是在我的代码中使用访问令牌和凭据。

我认为您的实现不适用于服务帐户,因为您使用的是“自己的帐户”,而不是服务帐户。请查看此处的代码示例作为参考。但是,当我调用
execute()
方法时,为什么google配额显示使用API图呢。它还显示了我在配额中从这段代码调用的API。你能告诉我如何在没有UI的情况下获取电子邮件吗?我的服务器上有cron,可以从gmail读取我的电子邮件。知道吗?我不使用cronOk。NP,但是有没有办法在没有UI的情况下获取电子邮件?