diff options
Diffstat (limited to 'oauth2client/contrib/django_util/__init__.py')
-rw-r--r-- | oauth2client/contrib/django_util/__init__.py | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/oauth2client/contrib/django_util/__init__.py b/oauth2client/contrib/django_util/__init__.py index 5449e32..644a8f9 100644 --- a/oauth2client/contrib/django_util/__init__.py +++ b/oauth2client/contrib/django_util/__init__.py @@ -52,6 +52,9 @@ Add the helper to your INSTALLED_APPS: This helper also requires the Django Session Middleware, so ``django.contrib.sessions.middleware`` should be in INSTALLED_APPS as well. +MIDDLEWARE or MIDDLEWARE_CLASSES (in Django versions <1.10) should also +contain the string 'django.contrib.sessions.middleware.SessionMiddleware'. + Add the client secrets created earlier to the settings. You can either specify the path to the credentials file in JSON format @@ -228,10 +231,10 @@ import importlib import django.conf from django.core import exceptions from django.core import urlresolvers -import httplib2 from six.moves.urllib import parse from oauth2client import clientsecrets +from oauth2client import transport from oauth2client.contrib import dictionary_storage from oauth2client.contrib.django_util import storage @@ -335,16 +338,26 @@ class OAuth2Settings(object): self.request_prefix = getattr(settings_instance, 'GOOGLE_OAUTH2_REQUEST_ATTRIBUTE', GOOGLE_OAUTH2_REQUEST_ATTRIBUTE) - self.client_id, self.client_secret = \ - _get_oauth2_client_id_and_secret(settings_instance) + info = _get_oauth2_client_id_and_secret(settings_instance) + self.client_id, self.client_secret = info + + # Django 1.10 deprecated MIDDLEWARE_CLASSES in favor of MIDDLEWARE + middleware_settings = getattr(settings_instance, 'MIDDLEWARE', None) + if middleware_settings is None: + middleware_settings = getattr( + settings_instance, 'MIDDLEWARE_CLASSES', None) + if middleware_settings is None: + raise exceptions.ImproperlyConfigured( + 'Django settings has neither MIDDLEWARE nor MIDDLEWARE_CLASSES' + 'configured') - if ('django.contrib.sessions.middleware.SessionMiddleware' - not in settings_instance.MIDDLEWARE_CLASSES): + if ('django.contrib.sessions.middleware.SessionMiddleware' not in + middleware_settings): raise exceptions.ImproperlyConfigured( - 'The Google OAuth2 Helper requires session middleware to ' - 'be installed. Edit your MIDDLEWARE_CLASSES setting' - ' to include \'django.contrib.sessions.middleware.' - 'SessionMiddleware\'.') + 'The Google OAuth2 Helper requires session middleware to ' + 'be installed. Edit your MIDDLEWARE_CLASSES or MIDDLEWARE ' + 'setting to include \'django.contrib.sessions.middleware.' + 'SessionMiddleware\'.') (self.storage_model, self.storage_model_user_property, self.storage_model_credentials_property) = _get_storage_model() @@ -470,8 +483,7 @@ class UserOAuth2(object): @property def http(self): - """Helper method to create an HTTP client authorized with OAuth2 - credentials.""" + """Helper: create HTTP client authorized with OAuth2 credentials.""" if self.has_credentials(): - return self.credentials.authorize(httplib2.Http()) + return self.credentials.authorize(transport.get_http_object()) return None |