没有足够的权限通过Gmail API删除邮件

时间:2014-08-15 10:31:58

标签: python debugging google-oauth gmail-api

我的代码应该在我的收件箱中收到一条消息,检查它是否有链接,然后从电子邮件中提取链接,然后将其删除。

message = gmail_service.users().messages().get(userId='me', id=thread['id'], format='raw').execute()    #get message using thread id
msg_str = base64.urlsafe_b64decode(message['raw'].encode('ASCII'))
msg = email.message_from_string(msg_str)
for part in msg.walk():
    msg.get_payload()
    if part.get_content_type() == 'text/html':
        mytext = base64.urlsafe_b64decode(part.get_payload().encode('UTF-8'))
        html = BeautifulSoup(mytext)
        for link in html.find_all('a'):
            mylink = link.get('href')
            print mylink
            try:
                service.users().messages().trash(userId='me', id=thread['id']).execute()
                print 'Message with id: %s trashed successfully.' % msg_id
            except errors.HttpError, error:
                print 'Could not trash ' + thread['id'] + ': %s' % error

我在代码中定义的范围是.modify,它应该为我提供删除邮件without fail所需的权限。为什么我收到错误:

<HttpError 403 when requesting https://www.googleapis.com/gmail/v1/users/me/messages/147d8c9a6348e437/trash?alt=json returned "Insufficient Permission">

修改:即使documented example返回403禁止错误,这也是Gmail API本身的错误。

2 个答案:

答案 0 :(得分:7)

在过去的某个时刻,您在同一目录中注册了readonly scope,并将其缓存在名为gmail.storage的文件中。

将该文件移开并重新运行程序,它应该可以正常工作。

答案 1 :(得分:1)

删除token.pickle文件。运行代码后,它将重新生成。运行代码时,将弹出一个浏览器窗口,以授权您的帐户和所用应用程序的权限。就像您执行“ quickstart”代码时一样。