Cleaned up OAuth 2.0 support fully using Storage() and updating samples.
diff --git a/samples/django_sample/manage.py b/samples/django_sample/manage.py
index bcdd55e..0b932da 100755
--- a/samples/django_sample/manage.py
+++ b/samples/django_sample/manage.py
@@ -4,7 +4,11 @@
import settings # Assumed to be in the same directory.
except ImportError:
import sys
- sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
+ sys.stderr.write("""Error: Can't find the file 'settings.py' in the
+directory containing %r. It appears you've customized things. You'll
+have to run django-admin.py, passing it your settings module.
+(If the file settings.py does indeed exist, it's causing an ImportError
+somehow.)\n""" % __file__)
sys.exit(1)
if __name__ == "__main__":
diff --git a/samples/oauth2/appengine/main.py b/samples/oauth2/appengine/main.py
index 981e392..78634e7 100644
--- a/samples/oauth2/appengine/main.py
+++ b/samples/oauth2/appengine/main.py
@@ -45,7 +45,8 @@
@login_required
def get(self):
user = users.get_current_user()
- credentials = StorageByKeyName(Credentials, user.user_id(), 'credentials').get()
+ credentials = StorageByKeyName(
+ Credentials, user.user_id(), 'credentials').get()
if credentials:
http = httplib2.Http()
@@ -87,7 +88,8 @@
flow = pickle.loads(memcache.get(user.user_id()))
if flow:
credentials = flow.step2_exchange(self.request.params)
- StorageByKeyName(Credentials, user.user_id(), 'credentials').put(credentials)
+ StorageByKeyName(
+ Credentials, user.user_id(), 'credentials').put(credentials)
self.redirect("/")
else:
pass
diff --git a/samples/oauth2/buzz/buzz.py b/samples/oauth2/buzz/buzz.py
index 7df6c72..fe30a32 100644
--- a/samples/oauth2/buzz/buzz.py
+++ b/samples/oauth2/buzz/buzz.py
@@ -11,28 +11,43 @@
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
-from apiclient.discovery import build
-from oauth2client.file import Storage
-
import httplib2
import pickle
import pprint
+from apiclient.discovery import build
+from oauth2client.file import Storage
+from oauth2client.client import OAuth2WebServerFlow
+from oauth2client.tools import run
+
# Uncomment the next line to get very detailed logging
#httplib2.debuglevel = 4
def main():
- credentials = Storage('buzz.dat').get()
+ storage = Storage('buzz.dat')
+ credentials = storage.get()
+ if not credentials:
+ flow = OAuth2WebServerFlow(
+ client_id='433807057907.apps.googleusercontent.com',
+ client_secret='jigtZpMApkRxncxikFpR+SFg',
+ scope='https://www.googleapis.com/auth/buzz',
+ user_agent='buzz-cmdline-sample/1.0',
+ domain='anonymous',
+ xoauth_displayname='Buzz Client Example App'
+ )
+ credentials = run(flow, storage)
http = httplib2.Http()
http = credentials.authorize(http)
- p = build("buzz", "v1", http=http, developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0")
+ p = build("buzz", "v1", http=http,
+ developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0")
activities = p.activities()
# Retrieve the first two activities
- activitylist = activities.list(max_results='2', scope='@self', userId='@me').execute()
+ activitylist = activities.list(
+ max_results='2', scope='@self', userId='@me').execute()
print "Retrieved the first two activities"
# Retrieve the next two activities
@@ -45,14 +60,16 @@
"data": {
'title': 'Testing insert',
'object': {
- 'content': u'Just a short note to show that insert is working. ☄',
+ 'content':
+ u'Just a short note to show that insert is working. ☄',
'type': 'note'}
}
}
activity = activities.insert(userId='@me', body=new_activity_body).execute()
print "Added a new activity"
- activitylist = activities.list(max_results='2', scope='@self', userId='@me').execute()
+ activitylist = activities.list(
+ max_results='2', scope='@self', userId='@me').execute()
# Add a comment to that activity
comment_body = {
diff --git a/samples/oauth2/buzz/web_server_dance.py b/samples/oauth2/buzz/web_server_dance.py
deleted file mode 100644
index d0e1f6f..0000000
--- a/samples/oauth2/buzz/web_server_dance.py
+++ /dev/null
@@ -1,37 +0,0 @@
-# Copyright (C) 2010 Google Inc.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-"""Do the OAuth 2.0 Web Server dance.
-
-Do the OAuth 2.0 Web Server dance for
-a command line application. Store the generated
-credentials in a common file that is used by
-other example apps in the same directory.
-"""
-
-__author__ = 'jcgregorio@google.com (Joe Gregorio)'
-
-from oauth2client.client import OAuth2WebServerFlow
-from oauth2client.tools import run
-
-flow = OAuth2WebServerFlow(
- client_id='433807057907.apps.googleusercontent.com',
- client_secret='jigtZpMApkRxncxikFpR+SFg',
- scope='https://www.googleapis.com/auth/buzz',
- user_agent='buzz-cmdline-sample/1.0',
- domain='anonymous',
- xoauth_displayname='Buzz Client Example App'
- )
-
-run(flow, 'buzz.dat')
diff --git a/samples/oauth2/latitude/latitude.py b/samples/oauth2/latitude/latitude.py
new file mode 100644
index 0000000..fc312dd
--- /dev/null
+++ b/samples/oauth2/latitude/latitude.py
@@ -0,0 +1,56 @@
+#!/usr/bin/python2.4
+# -*- coding: utf-8 -*-
+#
+# Copyright 2010 Google Inc. All Rights Reserved.
+
+"""Simple command-line example for Latitude.
+
+Command-line application that sets the users
+current location.
+"""
+
+__author__ = 'jcgregorio@google.com (Joe Gregorio)'
+
+import httplib2
+
+from apiclient.discovery import build
+from oauth2client.file import Storage
+from oauth2client.client import OAuth2WebServerFlow
+from oauth2client.tools import run
+
+# Uncomment to get detailed logging
+#httplib2.debuglevel = 4
+
+
+def main():
+ storage = Storage('latitude.dat')
+ credentials = storage.get()
+
+ if not credentials:
+ flow = OAuth2WebServerFlow(
+ client_id='433807057907.apps.googleusercontent.com',
+ client_secret='jigtZpMApkRxncxikFpR+SFg',
+ scope='https://www.googleapis.com/auth/latitude',
+ user_agent='latitude-cmdline-sample/1.0',
+ xoauth_displayname='Latitude Client Example App')
+
+ credentials = run(flow, storage)
+
+ http = httplib2.Http()
+ http = credentials.authorize(http)
+
+ p = build("latitude", "v1", http=http)
+
+ body = {
+ "data": {
+ "kind": "latitude#location",
+ "latitude": 37.420352,
+ "longitude": -122.083389,
+ "accuracy": 130,
+ "altitude": 35
+ }
+ }
+ print p.currentLocation().insert(body=body).execute()
+
+if __name__ == '__main__':
+ main()
diff --git a/samples/oauth2/moderator/moderator.py b/samples/oauth2/moderator/moderator.py
index 9a3dbb2..5a6e2b8 100644
--- a/samples/oauth2/moderator/moderator.py
+++ b/samples/oauth2/moderator/moderator.py
@@ -11,22 +11,30 @@
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
+import httplib2
from apiclient.discovery import build
from oauth2client.file import Storage
-
-import httplib2
+from oauth2client.client import OAuth2WebServerFlow
+from oauth2client.tools import run
# Uncomment to get low level HTTP logging
#httplib2.debuglevel = 4
-# Uncomment to get logging
-#import logging
-#logging.basicConfig(level=logging.DEBUG)
-
def main():
- credentials = Storage('moderator.dat').get()
+ storage = Storage('moderator.dat')
+ credentials = storage.get()
+
+ if not credentials:
+ flow = OAuth2WebServerFlow(
+ client_id='433807057907.apps.googleusercontent.com',
+ client_secret='jigtZpMApkRxncxikFpR+SFg',
+ scope='https://www.googleapis.com/auth/moderator',
+ user_agent='moderator-cmdline-sample/1.0',
+ xoauth_displayname='Moderator Client Example App')
+
+ credentials = run(flow, storage)
http = httplib2.Http()
http = credentials.authorize(http)
diff --git a/samples/oauth2/moderator/web_server_dance.py b/samples/oauth2/moderator/web_server_dance.py
deleted file mode 100644
index b3b9d35..0000000
--- a/samples/oauth2/moderator/web_server_dance.py
+++ /dev/null
@@ -1,35 +0,0 @@
-# Copyright (C) 2010 Google Inc.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-"""Do the OAuth 2.0 Web Server dance.
-
-Do the OAuth 2.0 Web Server dance for
-a command line application. Store the generated
-credentials in a common file that is used by
-other example apps in the same directory.
-"""
-
-__author__ = 'jcgregorio@google.com (Joe Gregorio)'
-
-from oauth2client.client import OAuth2WebServerFlow
-from oauth2client.tools import run
-
-flow = OAuth2WebServerFlow(
- client_id='433807057907.apps.googleusercontent.com',
- client_secret='jigtZpMApkRxncxikFpR+SFg',
- scope='https://www.googleapis.com/auth/moderator',
- user_agent='moderator-cmdline-sample/1.0',
- xoauth_displayname='Moderator Client Example App')
-
-run(flow, 'moderator.dat')
diff --git a/samples/oauth2/urlshortener/main.py b/samples/oauth2/urlshortener/main.py
new file mode 100644
index 0000000..f995f38
--- /dev/null
+++ b/samples/oauth2/urlshortener/main.py
@@ -0,0 +1,59 @@
+#!/usr/bin/python2.4
+# -*- coding: utf-8 -*-
+#
+# Copyright 2010 Google Inc. All Rights Reserved.
+
+"""Simple command-line example for Google URL Shortener API.
+
+Command-line application that shortens a URL.
+"""
+
+__author__ = 'jcgregorio@google.com (Joe Gregorio)'
+
+import httplib2
+import pprint
+
+from apiclient.discovery import build
+from oauth2client.file import Storage
+from oauth2client.client import OAuth2WebServerFlow
+from oauth2client.tools import run
+
+# Uncomment to get detailed logging
+#httplib2.debuglevel = 4
+
+
+def main():
+ storage = Storage('urlshortener.dat')
+ credentials = storage.get()
+
+ if not credentials:
+ flow = OAuth2WebServerFlow(
+ client_id='433807057907.apps.googleusercontent.com',
+ client_secret='jigtZpMApkRxncxikFpR+SFg',
+ scope='https://www.googleapis.com/auth/urlshortener',
+ user_agent='urlshortener-cmdline-sample/1.0',
+ xoauth_displayname='URL Shortener Client Example App')
+
+ credentials = run(flow, storage)
+
+ http = httplib2.Http()
+ http = 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)
+
+ shortUrl = resp['id']
+
+ # Convert the shortened URL back into a long URL
+ resp = url.get(shortUrl=shortUrl).execute()
+ pprint.pprint(resp)
+
+if __name__ == '__main__':
+ main()