如何使用 Python 和 Gmail API 阅读电子邮件

问题描述 投票:0回答:0

我想用 python 代码从我的收件箱中获取未读邮件。 我设置了谷歌开发者帐户,制作了一个应用程序(我将其设置为桌面)并下载了凭据。

{"installed":{"client_id":"xxx",
              "project_id":"xxx",
              "auth_uri":"https://accounts.google.com/o/oauth2/auth",
              "token_uri":"https://oauth2.googleapis.com/token",
              "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
              "client_secret":"xxx",
              "redirect_uris":["http://localhost"]
              }
}

这是我的代码:

import os
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build

creds = Credentials.from_authorized_user_file(os.path.expanduser('gmail_credencials.json'), ['https://www.googleapis.com/auth/gmail.readonly'])
service = build('gmail', 'v1', credentials=creds)
print(service)
messages = service.users().messages()
print(messages)

但是我收到这个错误:

ValueError: Authorized user info was not in the expected format, missing fields refresh_token, client_secret, client_id.

我有

client_secret
client_id
,但我不知道我应该从哪里得到
refresh_token
.

还有其他人遇到过这个错误吗?

python gmail gmail-imap
© www.soinside.com 2019 - 2024. All rights reserved.