Ошибка 401 при попытке удалить сообщения gmail через API - PullRequest
0 голосов
/ 03 апреля 2020

Я получил вдохновение от https://developers.google.com/gmail/api/quickstart/python и https://developers.google.com/gmail/api/v1/reference/users/labels/list#examples, чтобы получить следующий код

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
from apiclient import errors
import requests as req

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://mail.google.com/']


def chunks(l, n):
    for i in range(0, len(l), n):
        yield l[i:i + n]


def ListMessagesWithLabels(service, user_id, label_ids=[]):
    """List all Messages of the user's mailbox with label_ids applied.

    Args:
      service: Authorized Gmail API service instance.
      user_id: User's email address. The special value "me"
      can be used to indicate the authenticated user.
      label_ids: Only return Messages with these labelIds applied.

    Returns:
      List of Messages that have all required Labels applied. Note that the
      returned list contains Message IDs, you must use get with the
      appropriate id to get the details of a Message.
    """
    try:
        response = service.users().messages().list(userId=user_id,
                                                   labelIds=label_ids).execute()
        messages = []
        if 'messages' in response:
            messages.extend(response['messages'])

        while 'nextPageToken' in response:
            page_token = response['nextPageToken']
            response = service.users().messages().list(userId=user_id,
                                                       labelIds=label_ids,
                                                       pageToken=page_token).execute()
            messages.extend(response['messages'])

        return messages
    except errors.HttpError as error:
        print
        'An error occurred: %s' % error


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 (label["id"].upper() != label["id"]):
                messages = ListMessagesWithLabels(service, 'me', [label['id']])
                if messages is not None:
                    usr_id = 'me'
                    ids = [message["id"] for message in messages]
                    for x in chunks(ids, 1000):
                        post_req = req.post(f"https://www.googleapis.com/gmail/v1/users/{usr_id}/messages/batchDelete", data={"ids":x})
                        if post_req.status_code == 200:
                            print("all good")


if __name__ == '__main__':
    main()

Цель - go через каждый ярлык и удалить все сообщения. Все мои POST-запросы были отклонены, потому что я не «авторизован», хотя при запуске программы я go через Аутентификацию в браузере и т. Д. c.

Как я должен сконструировать мои POST для быть в состоянии достичь того, что я хочу сделать?

1 Ответ

1 голос
/ 03 апреля 2020

При попытке удалить сообщения вы должны использовать service, который вы создали ранее. Вот как вы аутентифицировались в первую очередь. При попытке использовать batchDelete здесь:

post_req = req.post(f"https://www.googleapis.com/gmail/v1/users/{usr_id}/messages/batchDelete", data={"ids":x})

Вы просто выполняете базовый c запрос к указанной конечной точке, вы не следуете процессу OAuth. А поскольку у вас нет доступа к ресурсу publi c, вы получаете ошибку авторизации.

Вместо этого вам следует использовать что-то вроде следующего:

messagesToDelete = {
  "ids": [
    "messageID1",
    "messageID2",
    # ... rest of messages to delete
  ]
}
service.users().messages().batchDelete(userId="me", body=messagesToDelete).execute()

Ссылка:

...