User Information Using OAuth with Google App Engine

When using OAuth 2.0 and Python, I want to have a user ID or email address to store / retrieve the OAuth access token, since I want to change the calendar even after the user has left.

There is so much documentation, and half of them are outdated (OAuth 1.0), that I could not figure it out.

I have the following code:

import webapp2 import os from apiclient.discovery import build from oauth2client.appengine import OAuth2DecoratorFromClientSecrets from google.appengine.api import oauth user_scope = 'https://www.googleapis.com/auth/userinfo.profile' decorator = OAuth2DecoratorFromClientSecrets( os.path.join(os.path.dirname(__file__), 'client_secrets.json'), scope=('https://www.googleapis.com/auth/calendar', user_scope) ) service = build('calendar', 'v3') class MainHandler(webapp2.RequestHandler): @decorator.oauth_required def get(self): self.response.write('Hello world!') user = oauth.get_current_user(user_scope) if user: self.response.write('%s\n' % user) self.response.write('- email = %s\n' % user.email()) self.response.write('- nickname = %s\n' % user.nickname()) self.response.write('- user_id = %s\n' % user.user_id()) else: self.response.write('No user found...') app = webapp2.WSGIApplication([ ('/', MainHandler), (decorator.callback_path, decorator.callback_handler()) ], debug=True) 

This works locally in a test environment, however, when I deploy it and run it on the network, I get the following error:

 Traceback (most recent call last): File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__ rv = self.handle_exception(request, response, e) File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__ rv = self.router.dispatch(request, response) File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher return route.handler_adapter(request, response) File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__ return handler.dispatch() File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch return self.handle_exception(e, self.app.debug) File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch return method(*args, **kwargs) File "/base/data/home/apps/s~myapp/oauth2client/appengine.py", line 714, in check_oauth resp = method(request_handler, *args, **kwargs) File "/base/data/home/apps/s~myapp/main.py", line 29, in get user = oauth.get_current_user('https://www.googleapis.com/auth/userinfo.profile') File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/oauth/oauth_api.py", line 100, in get_current_user _maybe_call_get_oauth_user(_scope) File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/oauth/oauth_api.py", line 231, in _maybe_call_get_oauth_user _maybe_raise_exception() File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/oauth/oauth_api.py", line 246, in _maybe_raise_exception raise NotAllowedError(error_detail) NotAllowedError 

What am I missing that causes this error?

+6
source share
1 answer

I only have something similar to my end. After you get the OAuth user, you need to save the current user ID. You can do this through the data warehouse or as a parameter if you use tasks

from google.appengine.api import users ClientID = users.get_current_user().user_id()

Then, with some code that you later need, OAuth tokens are executed.

 from oauth2client.appengine import CredentialsModel from oauth2client.appengine import StorageByKeyName credentials = StorageByKeyName(CredentialsModel, ClientID, 'credentials').get() cal = build('calendar', 'v3',http = credentials.authorize(httplib2.Http())) 

Then use cal to make your API calls.

+4
source

Source: https://habr.com/ru/post/975737/


All Articles