簡體   English   中英

使用 Google API 在 Python 腳本中發送電子郵件

[英]Use Google API to send emails in Python script

我想使用 Google API 發送電子郵件。 我復制了代碼

https://developers.google.com/gmail/api/quickstart/

https://developers.google.com/gmail/api/guides/sending

但是,我只能閱讀電子郵件的標簽,但不允許發送電子郵件。 這是它返回的錯誤:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}

這是我的代碼

from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from google.auth.transport.requests import Request

SCOPES = [
    'https://www.googleapis.com/auth/userinfo.email',
    'https://www.googleapis.com/auth/userinfo.profile',
    # Add other requested scopes.
    'https://www.googleapis.com/auth/gmail.send'
]

def create_message(sender, to, subject, message_text):
    message = MIMEText(message_text)
    message['to'] = to
    message['from'] = sender
    message['subject'] = subject
    return {'raw': message.as_string()}

def send_message(service, user_id, message):
    """
    Sends an email message.
    Arguments:
    service: an authorized Gmail API service instance.
    user_id: User's email address. To indicate the authenticated user, the special value "me" can be used.
    message: Message to be sent.
    """
    message = (service.users().messages().send(userId=user_id, body=message)
               .execute())
    print('Message Id: %s' % message['id'])
    return message

def main():
    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
    user_id = "me"
    results = service.users().labels().list(userId=user_id).execute()
    labels = results.get('labels', [])
    print("labels: ", labels)
    message = create_message(user_id, "foo@gmail.com", "credit", "API test only!")
    message = send_message(service, user_id, message)
    print(message)

if __name__ == '__main__':
    main()

在 SCOPES (l9) 中添加行以獲得發送授權后,您是否刪除了“token.pickle”文件? 您必須重做授權程序,否則您仍然只有讀取權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM