HTTP 错误 403 "Insuficiente permision"

HTTPERROR 403 "Insuficiente permision"

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

HTTPERROR 403 “Insuficiente permision”

代码

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()

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

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

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

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

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

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

解决方案

检查您在此行中设置的范围

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

确保添加

然后 运行 代码再次请求用户同意。