如何使用 Gmail api 发送 .eml 文件?

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

我试着写这个简单的脚本:

from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
service=build('gmail', 'v1', credentials=Credentials.from_authorized_user_file('/arpa/y/ytrezq/code_secret_client_672177239954-t5k5b84556kjohucmbirkrv2f9cbagt3.apps.googleusercontent.com.json', ['https://www.googleapis.com/auth/gmail.send']))
from sys import argv
f=open(argv[1], "r", encoding="utf-8")
from io import BytesIO
from googleapiclient.http import MediaIoBaseUpload
print(service.users().messages().send(userId='me',body={}, media_body=MediaIoBaseUpload(BytesIO(f.read().encode('utf-8')), mimetype='message/rfc6710', resumable=True)).execute())
f.close()

但是身份验证在

.send()
步骤失败:

ytrezq@odin /arpa/y/ytrezq % python resend.py request.eml
Traceback (most recent call last):
  File "/sdfeu/arpa/y/ytrezq/relance.py", line 8, in <module>
    print(service.users().messages().send(userId='me',body={}, media_body=MediaIoBaseUpload(BytesIO(f.read().encode('utf-8')), mimetype='message/rfc6710', resumable=True)).execute())
  File "/arpa/y/ytrezq/.local/lib/python3.9/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/arpa/y/ytrezq/.local/lib/python3.9/site-packages/googleapiclient/http.py", line 902, in execute
    _, body = self.next_chunk(http=http, num_retries=num_retries)
  File "/arpa/y/ytrezq/.local/lib/python3.9/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/arpa/y/ytrezq/.local/lib/python3.9/site-packages/googleapiclient/http.py", line 1007, in next_chunk
    resp, content = _retry_request(
  File "/arpa/y/ytrezq/.local/lib/python3.9/site-packages/googleapiclient/http.py", line 191, in _retry_request
    resp, content = http.request(uri, method, *args, **kwargs)
  File "/arpa/y/ytrezq/.local/lib/python3.9/site-packages/google_auth_httplib2.py", line 209, in request
    self.credentials.before_request(self._request, method, uri, request_headers)
  File "/arpa/y/ytrezq/.local/lib/python3.9/site-packages/google/auth/credentials.py", line 135, in before_request
    self.refresh(request)
  File "/arpa/y/ytrezq/.local/lib/python3.9/site-packages/google/oauth2/credentials.py", line 335, in refresh
    ) = reauth.refresh_grant(
  File "/arpa/y/ytrezq/.local/lib/python3.9/site-packages/google/oauth2/reauth.py", line 349, in refresh_grant
    _client._handle_error_response(response_data, retryable_error)
  File "/arpa/y/ytrezq/.local/lib/python3.9/site-packages/google/oauth2/_client.py", line 69, in _handle_error_response
    raise exceptions.RefreshError(
google.auth.exceptions.RefreshError: ('invalid_scope: Bad Request', {'error': 'invalid_scope', 'error_description': 'Bad Request'})

我做错了什么?

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