Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insufficient permissions to trash a message via the Gmail API

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.

like image 275
LeonH Avatar asked Aug 15 '14 10:08

LeonH


People also ask

Can I use Gmail API for free?

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.


2 Answers

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.

like image 147
Mike Schiraldi Avatar answered Sep 28 '22 09:09

Mike Schiraldi


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.

like image 28
Dpcaxx Avatar answered Sep 28 '22 09:09

Dpcaxx