5

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.

2 Answers 2

7

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.

1
  • 2
    Where/what is the gmail.storage file? Commented Jun 14, 2016 at 10:19
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.