【问题标题】:Oauth aware flow issueOauth 感知流程问题
【发布时间】:2017-11-10 18:31:33
【问题描述】:

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

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

具体下指南中的代码: 在以下代码 sn-p 中,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)

【问题讨论】:

    标签: python google-app-engine oauth


    【解决方案1】:

    通过阅读源代码,我认为“装饰器”应该先用于装饰某物,然后再使用它来做其他事情。在这种情况下,您可能想用oauth_aware 装饰get

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

    【讨论】:

      猜你喜欢
      • 2022-09-30
      • 1970-01-01
      • 2016-08-01
      • 1970-01-01
      • 2015-10-24
      • 2022-06-17
      • 2020-05-17
      • 2011-09-29
      • 1970-01-01
      相关资源
      最近更新 更多