Oauth意识到流动问题

时间:2014-07-25 17:46:16

标签: python google-app-engine oauth

问题陈述:在oauth感知装饰器上调用方法会导致非类型错误。

我看这个页面 https://developers.google.com/api-client-library/python/guide/google_app_engine

特别是指南中的代码: 在以下代码片段中,OAuth2DecoratorFromClientSecrets类用于创建oauth_aware装饰器,装饰器应用于访问Google Tasks API的函数:

尝试创建类似的东西(包含在下面)。

我的应用正在发出此错误

 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/APPNAME/DIRECTORYNAME/main.py", line 39, in get
    url = decorator.authorize_url()
  File "/base/data/home/apps/APPNAME/DIRECTORYNAME/oauth2client/appengine.py", line 798, in authorize_url
    url = self.flow.step1_get_authorize_url()
AttributeError: 'NoneType' object has no attribute 'step1_get_authorize_url'

当我执行logging.info(decorator)时,它返回

当我做的时候

>>>logging.info(dir(decorator))
>>> ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_auth_uri', '_callback_path', '_client_id', '_client_secret', '_create_flow', '_credentials_class', '_credentials_property_name', '_display_error_message', '_in_error', '_kwargs', '_message', '_revoke_uri', '_scope', '_storage_class', '_tls', '_token_response_param', '_token_uri', '_user_agent', 'authorize_url', 'callback_application', 'callback_handler', 'callback_path', 'credentials', 'flow', 'get_credentials', 'get_flow', 'has_credentials', 'http', 'oauth_aware', 'oauth_required', 'set_credentials', 'set_flow']

但是decorator.http()或decorator.has_credentials()等任何方法都会触发无类型错误

我的代码

import webapp2
import logging
import jinja2
import pprint
import os
import json
import time
import httplib2

from apiclient.discovery import build
from apiclient.errors import HttpError
from google.appengine.ext.webapp.util import run_wsgi_app

from oauth2client.appengine import OAuth2DecoratorFromClientSecrets
from oauth2client.client import AccessTokenRefreshError
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run

from google.appengine.api import urlfetch

decorator = OAuth2DecoratorFromClientSecrets(
    os.path.join(os.path.dirname(__file__), 'client_secrets.json'),
    scope='https://www.googleapis.com/auth/bigquery')

# Google App Engine project ID
PROJECT_NUMBER = 'XXXXXXXXXXXXX'
bigquery_service = build('bigquery', 'v2')

class MainHandler(webapp2.RequestHandler):
    def get(self):
        if decorator.has_credentials():
            logging.info('has credentials')
        else:
            logging.info('bouncing credentials')
            logging.info(decorator)
            url = decorator.authorize_url()
            return self.redirect(url)

        jinja_environment = self.jinja_environment
        template = jinja_environment.get_template("/index.html")
        self.response.out.write(template.render())

    @property
    def jinja_environment(self):
        jinja_environment = jinja2.Environment(
            loader=jinja2.FileSystemLoader('views')

        )
        return jinja_environment

app = webapp2.WSGIApplication([
    ('/', MainHandler),
    (decorator.callback_path, decorator.callback_handler()),
    ], debug=True)

1 个答案:

答案 0 :(得分:2)

通过仔细阅读源代码,我认为应该使用“装饰器”来装饰某些东西,然后再用它来做其他事情。在这种情况下,您可能希望使用oauth_aware装饰get

class MainHandler(webapp2.RequestHandler):
    @decorator.oauth_aware
    def get(self):
        if decorator.has_credentials():
            ...
        else:
            ...
            url = decorator.authorize_url()