与Gmail API 403错误发送电子邮件(蟒蛇) [英] 403 error sending email with gmail API (python)

查看:738
本文介绍了与Gmail API 403错误发送电子邮件(蟒蛇)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发送使用python的Gmail的API的电子邮件。我想,我也跟着的相关文件和YouTube的VID。

I'm trying to send an email using the Gmail API in python. I think I followed the relevant documentation and youtube vids.

我遇到这个错误:


googleapiclient.errors.HttpError: HttpError 403 when requesting https://www.googleapis.com/gmail/v1/users/me/messages/send?alt=json returned "Insufficient Permission"

下面是我的脚本:



    #!/usr/bin/env python

    from googleapiclient.discovery import build
    from httplib2 import Http
    from oauth2client import file, client, tools
    from email.mime.text import MIMEText
    import base64
    import errors

    SCOPES = 'https://mail.google.com/'
    CLIENT_SECRET = 'client_secret.json'

    store = file.Storage('storage.json')
    credz = store.get()
    if not credz or credz.invalid:
        flags = tools.argparser.parse_args(args=[])
        flow = client.flow_from_clientsecrets(CLIENT_SECRET, SCOPES)
        credz = tools.run_flow(flow, store, flags)

    GMAIL = build('gmail', 'v1', http=credz.authorize(Http()))

    def CreateMessage(sender, to, subject, message_text):
      """Create a message for an email.

      Args:
        sender: Email address of the sender.
        to: Email address of the receiver.
        subject: The subject of the email message.
        message_text: The text of the email message.

      Returns:
        An object containing a base64url encoded email object.
      """
      message = MIMEText(message_text)
      message['to'] = to
      message['from'] = sender
      message['subject'] = subject
      return {'raw': base64.urlsafe_b64encode(message.as_string())}

    def SendMessage(service, user_id, message):
      """Send an email message.

      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.
        message: Message to be sent.

      Returns:
        Sent Message.
      """
      try:
        message = (service.users().messages().send(userId=user_id, body=message)
                   .execute())
        print 'Message Id: %s' % message['id']
        return message
      except errors.HttpError, error:
        print 'An error occurred: %s' % error

    message = CreateMessage('testemail@gmail.com', 'testemail@gmail.com', 'test_subject', 'foo')
    print message
    SendMessage(GMAIL, 'me', message)

我尝试添加范围,尝试不同的电子邮件等我已经登录到我的浏览器,以及验证。 (该testemail@gmail.com是一个虚拟的电子邮件BTW)

I tried adding scopes, trying different emails, etc. I have authenticated by logging into my browser as well. (The testemail@gmail.com is a dummy email btw)

推荐答案

尝试删除生成的storage.json文件,然后重新再试。

Try deleting generated storage.json file and then try again afresh.

您可能会尝试这个剧本不同范围,以使storage.json可能有错误的详细信息。

you might be trying this script with different scopes so "storage.json" might be having wrong details.

这篇关于与Gmail API 403错误发送电子邮件(蟒蛇)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆