当前位置: 首页 > 面试题库 >

如何解决HttpError 403权限不足?(Gmail API,Python)

贺雅健
2023-03-14
问题内容

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

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。有谁知道我需要做些什么才能克服此错误?我对此完全是个菜鸟,因此,如果我对此不太满意,请原谅。谢谢!


问题答案:

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

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

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

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



 类似资料:
  • 我的Dockerfile配置: 报错信息: 我已经尝试过以下几种方法: Dockerfile配置中添加RUN chmod -R 777 /yice Dockerfile配置添加 USER root,它会报: 找不Chrome,然后因为ghcr.io/puppeteer/puppeteer:latest镜像切换的用户名为pptruser,所以我手动在代码里给puppeteer配置executable

  • 情况: 我正在为我的应用测试Gmail API。 我已经测试了一些请求,它们工作正常。例如,获取消息、获取用户历史记录、获取草稿列表等。。 基本上所有只读请求都工作正常。 相反,我有一些与其他请求的权限相关的问题,例如当我必须编写或删除草稿时。 这是我得到的错误: 代码: 这是初始化应用程序的函数: 这是删除一份草稿的请求: 编辑: 我已尝试撤销权限并设置新凭据。初始化服务时声明的范围是: 如文件

  • 本文向大家介绍PHP调用Linux命令权限不足问题解决方法,包括了PHP调用Linux命令权限不足问题解决方法的使用技巧和注意事项,需要的朋友参考一下 业务背景:  yourcmd为我的linux程序,它对权限要求非常严格,当用php去执行yourcmd程序 系统:CentOS 6.3 apache是php的执行用户 用exec函数去执行linux系统上的程序/usr/local/yourcmd/

  • 我只是因为将liquibase作为依赖项而收到了这个错误消息 原因:liquibase.exception.数据库异常: ORA-01031:权限不足[失败SQL:(1031)CREATE TABLE MYSCHMA. DATABASE CHANGELOGLOCK(ID INTEGER Not NULL, LOCKED NUMBER(1)Not NULL, LOCKGRANTED TIMESTAM

  • 我有以下规则: 所以基本上,我希望经过身份验证的用户能够读/写和集合。但当我创建一个用户并登录时,我会尝试写: 但我有一个错误: 不知道我做错了什么。任何帮助都将不胜感激——谢谢!

  • 我使用的是Role(允许读,写:如果request.auth.uid!=null),当我登录时,我得到的数据是ok的,但当我注销用户时,我得到的错误是:缺少或不充分的权限。首先,我认为这是因为我没有取消订阅我尝试的可观察的(rxjs/operator/takewhile)即使我使用异步管道,我也得到了同样的错误。