Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caller does not have permission error when using GMail API

I get 500 server error on my django website thats running on Google App Engine. When I look at Google App Engine logs I see the following error:

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest returned "The caller does not have permission">

When I hard refresh the browser this error goes away. Then after some time it pops back up. Happens on mobile(firefox, safari), laptop (firefox,chrome).

UPDATE:

In Django settings.py I have following code. Its last line generates the error :

pickle_path = 'token.pickle'  # path to token.pickle
with open(pickle_path, 'rb') as token:
    creds = pickle.load(token)
SERVICE = build('gmail', 'v1', credentials=creds)  # ERROR LINE

When I run the django server locally: I get following error:

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest returned "The caller does not have permission">

When I restart cloud sql connection from my terminal - the error is gone. This never used to happen until a few days ago.

like image 266
Aseem Avatar asked Jul 06 '20 03:07

Aseem


1 Answers

Here's a workaround:

  • Download the discovery_doc directly from google here
  • Load the json file (you can name it gmail-api.json)
  • Build from this json file using build_from_document

Before

    from googleapiclient.discovery import build

    gmail_creds = get_service_account_creds()
    gmail_service = build('gmail', 'v1', credentials=gmail_creds)

After

    from googleapiclient.discovery import build_from_document

    discovery_doc = load_json('config/gmail-api.json')
    gmail_creds = get_service_account_creds()

    gmail_service = build_from_document(discovery_doc, credentials=gmail_creds)

It seems that it is now a P0 for Google, so hopefully it will be fixed soon.

https://issuetracker.google.com/issues/160441983

like image 107
Pierre Delarroqua Avatar answered Oct 05 '22 23:10

Pierre Delarroqua