My code should take a message in my inbox, check it for a link and then pull the link from the email and then trash it.
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
The scope I have defined in my code is .modify
which should give me the permissions I need to trash a message without fail. Why am I getting the error:
<HttpError 403 when requesting https://www.googleapis.com/gmail/v1/users/me/messages/147d8c9a6348e437/trash?alt=json returned "Insufficient Permission">
Edit: Even the documented example returns a 403 forbidden error, this must be a bug with the Gmail API itself.
Gmail API is available for free, but it has certain daily usage limits for API calls. Daily usage: 1 billion API calls per day. Per User Rate Limit: 250 API calls per user per second.
At some point in the past, you registered with readonly scope in the same directory, and this was cached in the file called gmail.storage.
Move that file out of the way and run your program again and it should work.
Delete the token.pickle file. It will regenerate after you run your code. When you run the code a browser window will pop-up to authorize permission for your account and the app you are using. Just like when you did the 'quickstart' code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With