Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google API credentials refresh failed

It's part of my code:

 if os.path.exists('token.pickle'):
                with open('token.pickle', 'rb') as token:
                    creds = pickle.load(token)
            if not creds or not creds.valid:
                if creds and creds.expired and creds.refresh_token:
                    creds.refresh(Request())

If creds expired it must be refresh. On Windows this part works, but on Linux I get an error (on the last string):

('invalid_scope: Some requested scopes were invalid. {invalid=[a, c, d, e, g, h, i, l, m, ., /, o, p, r, s, t, u, v, w, :]}', '{\n  "error": "invalid_scope",\n  "error_description": "Some requested scopes were invalid. {invalid\\u003d[a, c, d, e, g, h, i, l, m, ., /, o, p, r, s, t, u, v, w, :]}",\n  "error_uri": "http://code.google.com/apis/accounts/docs/OAuth2.html"\n}')
like image 379
Art Avatar asked Feb 24 '26 21:02

Art


1 Answers

I am a python beginner and I had a similar problem. I edited https://developers.google.com/drive/api/v3/quickstart/python to be able to create events. I changed line with scope to:

SCOPES = 'https://www.googleapis.com/auth/calendar'

At first, my app works well, but then it is strange. Next day, it crashed and I have to delete token to be able to run the app again. When I run it I have to allow access (via opened browser) again. Then it works well, but next day again not;)

The problem is that the token expires in 3600s. That is the reason for the strange behaviour. When the token expires, it runs code for refresh existing token (creds.refresh(Request())), but on Windows I get an error:

RefreshError('invalid_scope: Some requested scopes were invalid. {invalid=[a, c, d, e, g, h, i, l, m, ., n, /, o, p, r, s, t, u, w, :]}', '{\n  "error": "invalid_scope",\n  "error_description": "Some requested scopes were invalid. {invalid\\u003d[a, c, d, e, g, h, i, l, m, ., n, /, o, p, r, s, t, u, w, :]}",\n  "error_uri": "http://code.google.com/apis/accounts/docs/OAuth2.html"\n}'),)

and app crash. From the error message I was confused.

When you delete token.pickle, it requires an autorization via browser again and after that the app works for another 3600s;) Many hours I was searching where is the problem.

Finally I found it. It should be:

SCOPES = ['https://www.googleapis.com/auth/calendar']

there were missing [ and ] brackets!!! It was causing the mentioned error.

Now it works for me.

Tip: when you want to test your app if the expiration and token refresh works, you can simply change the system clock to some future date.

like image 139
Honza K. Avatar answered Feb 27 '26 10:02

Honza K.