Как загружать вложения из Gmail через Google API с помощью PythonPython

Программы на Python
Anonymous
Как загружать вложения из Gmail через Google API с помощью Python

Сообщение Anonymous »

Я хочу загрузить все вложения, которые я получил от определенного адреса электронной почты за последние 12 часов, через платформу Google API с использованием Python. Я использую следующий код.

Код: Выделить всё

import base64
from datetime import datetime, timedelta
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
import json
from google.oauth2.service_account import Credentials

# Load service account credentials from JSON file
creds = Credentials.from_service_account_file(r'token.json',
scopes['https://www.googleapis.com/auth/gmail.readonly'])

# Create Gmail API service
service = build('gmail', 'v1', credentials=creds)

# Calculate the date 12 hours ago from now
now = datetime.utcnow()
time_threshold = now - timedelta(hours=12)
formatted_time_threshold = time_threshold.strftime('%Y-%m-%dT%H:%M:%S.%fZ')

# Define the email address of the sender you want to filter by
sender_email = 'mail@domain.com'

# Define the query to retrieve messages from the sender received in the last 12 hours
query = f'from:{sender_email} after:{formatted_time_threshold}'
print(query)

try:
# Get messages that match the query
response = service.users().messages().list(q=query, userId='me').execute()
messages = response.get('messages', [])
# Iterate through messages and download attachments
for msg in messages:
message = service.users().messages().get(userId='me', id=msg['id']).execute()
payload = message['payload']

# Check if message has any attachments
if 'parts' in payload:
for part in payload['parts']:
# Check if part is an attachment
if part['filename']:
filename = part['filename']
data = part['body']['data']
file_data = base64.urlsafe_b64decode(data.encode('UTF-8'))

# Save attachment to local disk
with open(filename, 'wb') as f:
f.write(file_data)
print(f'Saved attachment: {filename}')
except HttpError as error:
print(f'An error occurred: {error}')
Когда я запускаю это, я получаю HttpError, а когда я проверяю URL-адрес ошибки, я получаю следующее сообщение.

Код: Выделить всё

  "error": {
"code": 401,
"message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"errors": [
{
"message": "Login Required.",
"domain": "global",
"reason": "required",
"location": "Authorization",
"locationType": "header"
}
],
"status": "UNAUTHENTICATED"}

Я не уверен, в чем здесь проблема. Я неправильно аутентифицируюсь с помощью API или мне нужно настроить что-то еще для этого процесса?
Файл token.json, который я использую для установления соединения, имеет следующую структуру

Код: Выделить всё

{
"type": "service_account",
"project_id": "mailer-attachments-download",
"private_key_id": "",
"private_key": "",
"client_email": "attachment-downloader@mailer-attachments-download.iam.gserviceaccount.com",
"client_id": "",
"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_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/attachment-downloader%40mailer-attachments-download.iam.gserviceaccount.com"
}

Мне хотелось бы знать, что я делаю неправильно или есть ли какие-либо другие альтернативные средства, с помощью которых я могу достичь своей цели.

Подробнее здесь: https://stackoverflow.com/questions/760 ... ing-python

Вернуться в «Python»