Приведенный ниже код представляет собой упрощение моего фактического кода ( он разбит на набор функций), но по сути он делает то же самое, что и мой полный код.
Код: Выделить всё
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
def main():
scope = ['https://www.googleapis.com/auth/gmail.settings.basic']
credentials_file = 'credentials.json'
token_file = f'token_test.json'
credentials = None
if os.path.exists(token_file):
credentials = Credentials.from_authorized_user_file(token_file, scope)
if not credentials or not credentials.valid:
if credentials and credentials.expired and credentials.refresh_token:
credentials.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(credentials_file, scope)
credentials = flow.run_local_server(port=0)
with open(token_file, 'w') as token:
token.write(credentials.to_json())
service = build('gmail', 'v1', credentials=credentials)
filter_body = {'criteria': {'from': 'example@example.com'}, 'action': {'removeLabelsIds': ['SPAM']}}
result = (
service.users()
.settings()
.filters()
.create(userId='me', body=filter_body)
.execute())
return result
if __name__ == '__main__':
main()
Код: Выделить всё
Traceback (most recent call last):
File "C:\DATA\WORK\Assets\Development\Python\GmailManager\src\temp.py", line 36, in
main()
File "C:\DATA\WORK\Assets\Development\Python\GmailManager\src\temp.py", line 31, in main
.execute())
^^^^^^^^^
File "C:\DATA\WORK\Assets\Development\Python\GmailManager\.venv\Lib\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\DATA\WORK\Assets\Development\Python\GmailManager\.venv\Lib\site-packages\googleapiclient\http.py", line 938, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError:
Подробнее здесь: [url]https://stackoverflow.com/questions/79012126/error-when-trying-add-filter-to-gmail-using-python-script[/url]