2014-09-23 20 views
6

Quando si utilizza OAuth 2.0 e Python, desidero avere l'ID utente o la posta elettronica per archiviare/recuperare il token di accesso OAuth poiché desidero modificare un calendario anche dopo che l'utente è andato via.Informazioni utente con OAuth con Google App Engine

C'è così tanta documentazione e metà è deprecata (OAuth 1.0) che non sono stato in grado di capirlo.

Ho il seguente codice:

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) 

questo funziona localmente nell'ambiente di prova, tuttavia quando schiero ed eseguirlo in linea ottengo il seguente errore:

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 

Che cosa sono io mancante che causa questo errore?

+0

probabilmente vale la pena abilitare la traccia http nelle librerie di google in modo da poter vedere cosa sta succedendo. Le librerie OAuth amano offuscare l'errore sottostante. La mia ipotesi sarebbe che il tuo URL di callback non sia corretto. – pinoyyid

+2

+1 per 'non c'è molta documentazione ma [è tutto schifo]'. In realtà è un rovescio sulla solita lamentela di nessuna documentazione. Qui è ago-in-pagliaio per trovare la pepita di informazioni che ti servono. –

risposta

4

Ho appena ottenuto qualcosa di simile lavorando alla mia estremità. Una volta ottenuto l'utente su OAuth, è necessario memorizzare l'ID utente corrente. È possibile farlo tramite datastore o come parametro se la vostra utilizzando code di attività

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

Poi, con quello che mai codice che successivamente necessario l'OAuth gettoni corrono.

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())) 

Quindi utilizzare il cal per effettuare le chiamate API.

+0

Grazie Sto usando questo, purtroppo c'è un bug che se sei loggato con 5 o più utenti ottieni 400 errori. A quanto pare questo bug non è stato corretto: https://code.google.com/p/googleappengine/issues/detail?id=9045 e https://twitter.com/googlecloud/status/467411184430239745 –

+0

Non lo sapevo grazie. Dovrò tenerlo d'occhio. – Ryan