【问题标题】:HTTPERROR 403 "Insuficiente permision" [closed]HTTP ERROR 403“权限不足”[关闭]
【发布时间】:2019-04-18 07:50:15
【问题描述】:

我正在尝试请求用户 gmail 数据列表消息并修改其中之一。我收到以下错误

HTTPERROR 403 “权限不足”

代码

def main():
    """Shows basic usage of the Gmail API.
    Lists the user's Gmail labels.
    """
    # The file token.json stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    store = file.Storage('token.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
        creds = tools.run_flow(flow, store)
    service = build('gmail', 'v1', http=creds.authorize(Http()))

    # Call the Gmail API
    results = service.users().labels().list(userId='me').execute()
    labels = results.get('labels', [])

    if not labels:
        print('No labels found.')
    else:
        print('Labels:')
        for label in labels:
            print(label['name'])
        response = service.users().messages().list(userId='me',
                                                labelIds='INBOX').execute()
        messages = []
        if 'messages' in response:
            messages.extend(response['messages'])
        #print(messages)
        for i in messages:
            aux_id = i
            id = aux_id.get('id')
            print(id)
            message = service.users().messages().get(userId='me', id=id).execute()
            #print (type(message))
            data = message.get('payload', {}).get('body',{}).get('data')
            data = data.decode('base64')
            list_of_dic = message.get('payload',{}).get('headers')
            #print(list_of_dic)
            #DATE, JUST NEED TO FORMAT IT TO YYYY/MM/DD
            for i in list_of_dic:
                if i['name'] == 'Date':
                    aux_date = i
                if i['name'] == 'From':
                    aux_sender = i
                if i['name'] == 'Subject':
                    aux_subject = i
            sender = aux_sender.get('value')
            date = aux_date.get('value')
            subject = aux_subject.get('value')
            print(date)
            print(sender)
            print(subject)
            print(data)## data from emaail working
            print('***************************************************')
            message = service.users().messages().modify(userId='me', id=id,
                                            body='INBOX').execute()

【问题讨论】:

  • 能否添加此错误的跟踪日志?
  • 欢迎来到 Stack Overflow。要问一个好问题,不仅仅是复制/粘贴您的代码和错误消息。请阅读How do I ask a good question 并改进您的问题。
  • @KlausD。我已经编辑了他的问题,我希望这些信息足以让您清楚地了解这个问题。通过阅读代码,我能够理解问题,但我添加了文本以使其更清晰。

标签: python google-api gmail-api google-api-python-client


【解决方案1】:

Insuficiente 权限可能意味着以下两种情况之一

  1. 与您进行身份验证的用户无权执行您正在尝试执行的操作。
  2. 用户未授予您的应用程序足够的权限来执行您正在尝试执行的操作。

当您使用我的用户 ID 时,我认为我们可以假设问题不是第一个问题。默认情况下,用户将有权访问他们的数据。问题在于您的应用程序请求和用户授予的权限范围。

messages.listmessages.get 需要以下范围之一才能工作。

但是messages.modify 需要以下范围之一才能工作。

您没有提到您要求的范围。我建议您检查范围并确保您已请求足够的权限来修改电子邮件。确保您对用户进行身份验证。

解决方案

检查您在此行中设置了 SCOPES 的内容

flow = client.flow_from_clientsecrets('credentials.json', SCOPES)

确保添加任一

然后再次运行代码,以便再次征求用户同意。

【讨论】:

  • 谢谢 DalmTo,但我不确定我如何添加范围,是在凭证文件上吗?
  • SCOPES
猜你喜欢
  • 2016-06-23
  • 2015-04-21
  • 1970-01-01
  • 2015-10-15
  • 2017-02-20
  • 2018-05-20
  • 1970-01-01
  • 2014-06-01
  • 2018-01-24
相关资源
最近更新 更多