我的代码应该从收件箱中获取一条消息,检查其中是否包含链接,然后提取邮件中的链接并将其删除。
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
,这应该给我所需的权限,以便在没有失败的情况下删除一条消息。为什么会出现错误:
<HttpError 403 when requesting https://www.googleapis.com/gmail/v1/users/me/messages/147d8c9a6348e437/trash?alt=json returned "Insufficient Permission">
编辑:即使是文档示例也会返回403禁止错误,这一定是Gmail API本身的一个bug。