如何解决 HttpError 403 权限不足? (gmail api, python)

How do I get around HttpError 403 Insufficient Permission? (gmail api, python)

我在执行代码时不断收到以下错误:

An error occurred: <HttpError 403 when requesting https://www.googleapis.com/gmail/v1/users/me/messages/send?alt=json returned "Insufficient Permission">

这是我的代码:

import httplib2
import os
from httplib2 import Http

from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

#SCOPES = 'https://www.googleapis.com/'
SCOPES = 'https://www.googleapis.com/auth/gmail.compose'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Gmail API Quickstart'

def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'gmail-quickstart.json')

    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatability with Python 2.6
            credentials = tools.run(flow, store)
        print 'Storing credentials to ' + credential_path
    return credentials

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 base64 encoded email object.
  """
  message = MIMEText(message_text)
  message['to'] = to
  message['from'] = sender
  message['subject'] = subject
  return {'raw': base64.b64encode(message.as_string())}

testMessage = CreateMessage('ENTER SENDERS EMAIL ADDRESS', 'ENTER RECEIVERRS EMAIL ADDRESS', 'ENTER SUBJECT', 'ENTER EMAIL BODY')

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


testSend = SendMessage(service, 'me', testMessage)

我一直在读到我需要编辑凭证文件,但我似乎找不到它。我安装了 windows 7。有谁知道我需要做什么才能克服这个错误?我对此完全是个菜鸟,所以如果我对此似乎有点菜鸟,请原谅。谢谢!

通过将 SCOPES 行更改为:

解决了这个问题
SCOPES = 'https://mail.google.com/'

电子邮件发送完美

尽管接受的答案是 100% 正确的。我认为值得指出为什么会这样。

授权gmail服务客户端时,可以指定几个不同的范围:全部、撰写、标签等...

这些都列在这里:https://developers.google.com/gmail/api/auth/scopes

答案中提到的范围提供完整的 gmail 访问权限。

Gmail API 具有以下范围:

对于发送电子邮件,https://www.googleapis.com/auth/gmail.send is needed or full access https://mail.google.com/

取自 here 的范围。

如果您运行之前是官方的“gmail-python-quickstart”,请删除您系统中的文件"gmail-quickstart.json"。重新运行 您的程序,以便您可以根据需要设置权限。

除了以下答案:

  1. ccy
  2. 阿帕达纳
  3. 拉格南皮扎

并作为 ccy 回答的促进...


解决方案 1...

... 黑客修复

如果您使用的是原始 gmail-python-quickstart 代码,请务必同时更新以下内容:

  1. CLIENT_SECRET_FILE = '/path/to/your/secret_client.json'
  2. 强制get_credentials()使用失败的凭据逻辑路径...
if True:
    flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
    flow.user_agent = APPLICATION_NAME
    if flags:
        credentials = tools.run_flow(flow, store, flags)
    else: # Needed only for compatibility with Python 2.6
        credentials = tools.run(flow, store)
    print('Storing credentials to ' + credential_path)

强制True这样逻辑运算肯定会工作client.flow运算:

if True:
    flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
    flow.user_agent = APPLICATION_NAME
    if flags:
        credentials = tools.run_flow(flow, store, flags)
    else: # Needed only for compatibility with Python 2.6
        credentials = tools.run(flow, store)
    print('Storing credentials to ' + credential_path)

这是一个 hacky 修复程序,但可以让您在短时间内开始使用。

问题...

  • 此方法的问题在于它强制使用 flow 代码,这会打开身份验证 window 浏览器并要求最终用户在发送电子邮件之前接受安全协议。
  • 这显然打破了自动生成和发送电子邮件的概念。

解决方案 2...

...稳定、自动化程度更高的解决方案

我发现执行以下操作:

  1. 将下载的 secret-client-####.html.json 文件复制到 get_credentials() 方法的第一段代码中定义的目录中。基本上,将其复制到您的 user/.credentials 目录
  2. 删除当前gmail-python-quickstart.json
  3. 将下载的文件重命名为 gmail-python-quickstart.json

运行 你的代码,然后它应该可以正常工作。

好处...

  • 认证页面不显示
  • 邮件自动发送

如果你使用google的官方示例,~/.credentials/目录中应该有一个旧文件夹,删除该目录中的所有内容并重新运行你的代码。然后你必须添加新的权限然后一切就OK了!