Moving all OAuth code and samples to use Storage consistently
diff --git a/samples/oauth2/appengine/main.py b/samples/oauth2/appengine/main.py
index 78634e7..f037bb8 100644
--- a/samples/oauth2/appengine/main.py
+++ b/samples/oauth2/appengine/main.py
@@ -48,21 +48,7 @@
credentials = StorageByKeyName(
Credentials, user.user_id(), 'credentials').get()
- if credentials:
- http = httplib2.Http()
- http = credentials.authorize(http)
- p = build("buzz", "v1", http=http)
- activities = p.activities()
- activitylist = activities.list(scope='@consumption',
- userId='@me').execute()
- path = os.path.join(os.path.dirname(__file__), 'welcome.html')
- logout = users.create_logout_url('/')
- self.response.out.write(
- template.render(
- path, {'activitylist': activitylist,
- 'logout': logout
- }))
- else:
+ if credentials is None or credentials.invalid == True:
flow = OAuth2WebServerFlow(
# Visit https://code.google.com/apis/console to
# generate your client_id, client_secret and to
@@ -78,6 +64,20 @@
authorize_url = flow.step1_get_authorize_url(callback)
memcache.set(user.user_id(), pickle.dumps(flow))
self.redirect(authorize_url)
+ else:
+ http = httplib2.Http()
+ http = credentials.authorize(http)
+ p = build("buzz", "v1", http=http)
+ activities = p.activities()
+ activitylist = activities.list(scope='@consumption',
+ userId='@me').execute()
+ path = os.path.join(os.path.dirname(__file__), 'welcome.html')
+ logout = users.create_logout_url('/')
+ self.response.out.write(
+ template.render(
+ path, {'activitylist': activitylist,
+ 'logout': logout
+ }))
class OAuthHandler(webapp.RequestHandler):
diff --git a/samples/oauth2/buzz/buzz.py b/samples/oauth2/buzz/buzz.py
index fe30a32..a009517 100644
--- a/samples/oauth2/buzz/buzz.py
+++ b/samples/oauth2/buzz/buzz.py
@@ -27,7 +27,7 @@
def main():
storage = Storage('buzz.dat')
credentials = storage.get()
- if not credentials:
+ if credentials is None or credentials.invalid == True:
flow = OAuth2WebServerFlow(
client_id='433807057907.apps.googleusercontent.com',
client_secret='jigtZpMApkRxncxikFpR+SFg',
diff --git a/samples/oauth2/django_sample/buzz/views.py b/samples/oauth2/django_sample/buzz/views.py
index b59a334..f6903b7 100644
--- a/samples/oauth2/django_sample/buzz/views.py
+++ b/samples/oauth2/django_sample/buzz/views.py
@@ -22,7 +22,7 @@
def index(request):
storage = Storage(CredentialsModel, 'id', request.user, 'credential')
credential = storage.get()
- if credential is None:
+ if credential is None or credential.invalid == True:
flow = OAuth2WebServerFlow(
client_id='837647042410.apps.googleusercontent.com',
client_secret='+SWwMCL9d8gWtzPRa1lXw5R8',
@@ -47,6 +47,7 @@
'activitylist': activitylist,
})
+
@login_required
def auth_return(request):
try:
diff --git a/samples/oauth2/latitude/latitude.py b/samples/oauth2/latitude/latitude.py
index fc312dd..e0458f4 100644
--- a/samples/oauth2/latitude/latitude.py
+++ b/samples/oauth2/latitude/latitude.py
@@ -26,7 +26,7 @@
storage = Storage('latitude.dat')
credentials = storage.get()
- if not credentials:
+ if credentials is None or credentials.invalid:
flow = OAuth2WebServerFlow(
client_id='433807057907.apps.googleusercontent.com',
client_secret='jigtZpMApkRxncxikFpR+SFg',
diff --git a/samples/oauth2/moderator/moderator.py b/samples/oauth2/moderator/moderator.py
index 5a6e2b8..ae6c6d3 100644
--- a/samples/oauth2/moderator/moderator.py
+++ b/samples/oauth2/moderator/moderator.py
@@ -26,7 +26,7 @@
storage = Storage('moderator.dat')
credentials = storage.get()
- if not credentials:
+ if credentials is None or credentials.invalid == True:
flow = OAuth2WebServerFlow(
client_id='433807057907.apps.googleusercontent.com',
client_secret='jigtZpMApkRxncxikFpR+SFg',
diff --git a/samples/oauth2/urlshortener/main.py b/samples/oauth2/urlshortener/main.py
index 41677fb..e129069 100644
--- a/samples/oauth2/urlshortener/main.py
+++ b/samples/oauth2/urlshortener/main.py
@@ -37,26 +37,6 @@
credentials = run(flow, storage)
-
-# # Test AccessTokenCredentials
-# at_credentials = AccessTokenCredentials(
-# credentials.access_token, 'urlshortener-cmdline-sample/1.0')
-# http = httplib2.Http()
-# http = at_credentials.authorize(http)
-#
-# # Build the url shortener service
-# service = build("urlshortener", "v1", http=http,
-# developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0")
-# url = service.url()
-#
-# # Create a shortened URL by inserting the URL into the url collection.
-# body = {"longUrl": "http://code.google.com/apis/urlshortener/" }
-# resp = url.insert(body=body).execute()
-# pprint.pprint(resp)
-# http = httplib2.Http()
-# http = credentials.authorize(http)
-#
-
http = httplib2.Http()
http = credentials.authorize(http)
@@ -77,7 +57,5 @@
pprint.pprint(resp)
-
-
if __name__ == '__main__':
main()