Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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 API:如何简单地验证用户并获取他们的消息列表?_Python_Google Api_Python Requests_Google Oauth_Gmail Api - Fatal编程技术网

Python Gmail API:如何简单地验证用户并获取他们的消息列表?

Python Gmail API:如何简单地验证用户并获取他们的消息列表?,python,google-api,python-requests,google-oauth,gmail-api,Python,Google Api,Python Requests,Google Oauth,Gmail Api,我正在尝试构建一个简单的python脚本来访问gmail的API,并将收件箱中的某些电子邮件组织成csv文件 我在下面的文档中看到,访问消息是使用用户(在本例中是我的)电子邮件地址完成的 我在访问API时遇到了困难。我得到以下错误: 请求缺少必需的身份验证凭据。需要OAuth 2访问令牌、登录cookie或其他有效身份验证凭据。请参阅 我希望构建一个轻量级的脚本,而不是web应用程序。有人知道我是否有办法在脚本中验证自己的电子邮件地址吗 PS:我想我可以使用selenium自动登录,但我想知道

我正在尝试构建一个简单的python脚本来访问gmail的API,并将收件箱中的某些电子邮件组织成csv文件

我在下面的文档中看到,访问消息是使用用户(在本例中是我的)电子邮件地址完成的

我在访问API时遇到了困难。我得到以下错误:

请求缺少必需的身份验证凭据。需要OAuth 2访问令牌、登录cookie或其他有效身份验证凭据。请参阅

我希望构建一个轻量级的脚本,而不是web应用程序。有人知道我是否有办法在脚本中验证自己的电子邮件地址吗


PS:我想我可以使用selenium自动登录,但我想知道是否有办法使用gmail的API实现这一点。

您需要了解您试图访问的数据是私人用户数据。这是属于您的用户的数据,这意味着您的应用程序需要由用户“您”进行“授权”“访问他们的数据

我们使用一个名为Oauth2的方法来实现这一点,在这种情况下,它将允许您的应用程序请求访问用户电子邮件的许可

为了使用Oauth2,您必须首先在上注册应用程序并设置一些东西,这将向Google标识您的应用程序

所有这些都在gmail的应用程序中进行了解释。完成该工作后,您应该能够将代码更改为使用message.list而不是labels.list

from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']

def main():
    """Shows basic usage of the Gmail API.
    Lists the user's Gmail labels.
    """
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('gmail', 'v1', credentials=creds)

    # Call the Gmail API
    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()

我不知道Google是否支持OAuth2以外的任何其他身份验证方法。您应该能够通过Python中的OAuth2登录到自己的帐户。文档中没有描述如何使用API进行身份验证吗?这可能会有所帮助吗。本文中建议在Python中为Google API提供一个客户端库(它们没有提到Python,但提到了“pip”)。