Finish adding Storage to the OAuth 1.0 client and updated all examples to use file.Storage. No more three_legged_dance.py files anywhere.
diff --git a/samples/buzz/buzz.py b/samples/buzz/buzz.py
index cf3581b..769057e 100644
--- a/samples/buzz/buzz.py
+++ b/samples/buzz/buzz.py
@@ -18,7 +18,6 @@
from apiclient.oauth import CredentialsInvalidError
import httplib2
-import pickle
import pprint
# Uncomment the next line to get very detailed logging
@@ -26,7 +25,8 @@
def main():
- credentials = Storage('buzz.dat').get()
+ storage = Storage('buzz.dat')
+ credentials = storage.get()
if credentials is None or credentials.invalid == True:
buzz_discovery = build("buzz", "v1").auth_discovery()
@@ -38,7 +38,7 @@
scope='https://www.googleapis.com/auth/buzz',
xoauth_displayname='Google API Client Example App')
- credentials = run(flow, 'buzz.dat')
+ credentials = run(flow, storage)
http = httplib2.Http()
http = credentials.authorize(http)
diff --git a/samples/latitude/latitude.py b/samples/latitude/latitude.py
index c2ec635..4ff5b03 100644
--- a/samples/latitude/latitude.py
+++ b/samples/latitude/latitude.py
@@ -27,7 +27,8 @@
def main():
- credentials = Storage('latitude.dat').get()
+ storage = Storage('latitude.dat')
+ credentials = storage.get()
if credentials is None or credentials.invalid == True:
auth_discovery = build("latitude", "v1").auth_discovery()
flow = FlowThreeLegged(auth_discovery,
@@ -45,7 +46,7 @@
granularity='city'
)
- credentials = run(flow, 'latitude.dat')
+ credentials = run(flow, storage)
http = httplib2.Http()
http = credentials.authorize(http)
diff --git a/samples/localdiscovery/buzz.py b/samples/localdiscovery/buzz.py
index 416f58c..8b797f7 100644
--- a/samples/localdiscovery/buzz.py
+++ b/samples/localdiscovery/buzz.py
@@ -11,10 +11,14 @@
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
+from apiclient.discovery import build
from apiclient.discovery import build_from_document
+from apiclient.oauth import FlowThreeLegged
+from apiclient.ext.authtools import run
+from apiclient.ext.file import Storage
+from apiclient.oauth import CredentialsInvalidError
import httplib2
-import pickle
import pprint
# Uncomment the next line to get very detailed logging
@@ -22,10 +26,20 @@
def main():
- # Load the credentials and authorize
- f = open("buzz.dat", "r")
- credentials = pickle.loads(f.read())
- f.close()
+ storage = Storage('buzz.dat')
+ credentials = storage.get()
+ if credentials is None or credentials.invalid == True:
+ buzz_discovery = build("buzz", "v1").auth_discovery()
+
+ flow = FlowThreeLegged(buzz_discovery,
+ consumer_key='anonymous',
+ consumer_secret='anonymous',
+ user_agent='python-buzz-sample/1.0',
+ domain='anonymous',
+ scope='https://www.googleapis.com/auth/buzz',
+ xoauth_displayname='Google API Client Example App')
+
+ credentials = run(flow, storage)
http = httplib2.Http()
http = credentials.authorize(http)
@@ -48,43 +62,15 @@
developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0")
activities = p.activities()
- # Retrieve the first two activities
- activitylist = activities.list(
- max_results='2', scope='@self', userId='@me').execute()
- print "Retrieved the first two activities"
+ try:
+ # Retrieve the first two activities
+ activitylist = activities.list(
+ max_results='2', scope='@self', userId='@me').execute()
+ print "Retrieved the first two activities"
+ except CredentialsInvalidError:
+ print 'Your credentials are no longer valid.'
+ print 'Please re-run this application to re-authorize.'
- # Retrieve the next two activities
- if activitylist:
- activitylist = activities.list_next(activitylist).execute()
- print "Retrieved the next two activities"
-
- # Add a new activity
- new_activity_body = {
- "data": {
- 'title': 'Testing insert',
- 'object': {
- '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()
-
- # Add a comment to that activity
- comment_body = {
- "data": {
- "content": "This is a comment"
- }
- }
- item = activitylist['items'][0]
- comment = p.comments().insert(
- userId=item['actor']['id'], postId=item['id'], body=comment_body
- ).execute()
- print 'Added a comment to the new activity'
- pprint.pprint(comment)
if __name__ == '__main__':
main()
diff --git a/samples/localdiscovery/three_legged_dance.py b/samples/localdiscovery/three_legged_dance.py
deleted file mode 100644
index 2dc26e5..0000000
--- a/samples/localdiscovery/three_legged_dance.py
+++ /dev/null
@@ -1,40 +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 1.0a three legged dance.
-
-Do the OAuth 1.0a three legged dance for
-a Buzz 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 apiclient.discovery import build
-from apiclient.oauth import FlowThreeLegged
-from apiclient.ext.authtools import run
-
-
-buzz_discovery = build("buzz", "v1").auth_discovery()
-
-flow = FlowThreeLegged(buzz_discovery,
- consumer_key='anonymous',
- consumer_secret='anonymous',
- user_agent='google-api-client-python-buzz-cmdline/1.0',
- domain='anonymous',
- scope='https://www.googleapis.com/auth/buzz',
- xoauth_displayname='Google API Client Example App')
-
-run(flow, 'buzz.dat')
diff --git a/samples/moderator/moderator.py b/samples/moderator/moderator.py
index 89ec9ca..c22f86e 100644
--- a/samples/moderator/moderator.py
+++ b/samples/moderator/moderator.py
@@ -13,18 +13,33 @@
from apiclient.discovery import build
+from apiclient.oauth import FlowThreeLegged
+from apiclient.ext.authtools import run
+from apiclient.ext.file import Storage
+from apiclient.oauth import CredentialsInvalidError
import httplib2
-import pickle
# Uncomment to get detailed logging
# httplib2.debuglevel = 4
def main():
- f = open("moderator.dat", "r")
- credentials = pickle.loads(f.read())
- f.close()
+ storage = Storage('moderator.dat')
+ credentials = storage.get()
+ if credentials is None or credentials.invalid == True:
+ moderator_discovery = build("moderator", "v1").auth_discovery()
+
+ flow = FlowThreeLegged(moderator_discovery,
+ consumer_key='anonymous',
+ consumer_secret='anonymous',
+ user_agent='google-api-client-python-mdrtr-cmdline/1.0',
+ domain='anonymous',
+ scope='https://www.googleapis.com/auth/moderator',
+ #scope='tag:google.com,2010:auth/moderator',
+ xoauth_displayname='Google API Client Example App')
+
+ credentials = run(flow, storage)
http = httplib2.Http()
http = credentials.authorize(http)
@@ -38,43 +53,47 @@
"videoSubmissionAllowed": False
}
}
- series = p.series().insert(body=series_body).execute()
- print "Created a new series"
+ try:
+ series = p.series().insert(body=series_body).execute()
+ print "Created a new series"
- topic_body = {
- "data": {
- "description": "Share your ideas on eating healthy!",
- "name": "Ideas",
- "presenter": "liz"
+ topic_body = {
+ "data": {
+ "description": "Share your ideas on eating healthy!",
+ "name": "Ideas",
+ "presenter": "liz"
+ }
}
- }
- topic = p.topics().insert(seriesId=series['id']['seriesId'],
- body=topic_body).execute()
- print "Created a new topic"
+ topic = p.topics().insert(seriesId=series['id']['seriesId'],
+ body=topic_body).execute()
+ print "Created a new topic"
- submission_body = {
- "data": {
- "attachmentUrl": "http://www.youtube.com/watch?v=1a1wyc5Xxpg",
- "attribution": {
- "displayName": "Bashan",
- "location": "Bainbridge Island, WA"
- },
- "text": "Charlie Ayers @ Google"
+ submission_body = {
+ "data": {
+ "attachmentUrl": "http://www.youtube.com/watch?v=1a1wyc5Xxpg",
+ "attribution": {
+ "displayName": "Bashan",
+ "location": "Bainbridge Island, WA"
+ },
+ "text": "Charlie Ayers @ Google"
+ }
}
- }
- submission = p.submissions().insert(seriesId=topic['id']['seriesId'],
- topicId=topic['id']['topicId'], body=submission_body).execute()
- print "Inserted a new submisson on the topic"
+ submission = p.submissions().insert(seriesId=topic['id']['seriesId'],
+ topicId=topic['id']['topicId'], body=submission_body).execute()
+ print "Inserted a new submisson on the topic"
- vote_body = {
- "data": {
- "vote": "PLUS"
+ vote_body = {
+ "data": {
+ "vote": "PLUS"
+ }
}
- }
- p.votes().insert(seriesId=topic['id']['seriesId'],
- submissionId=submission['id']['submissionId'],
- body=vote_body)
- print "Voted on the submission"
+ p.votes().insert(seriesId=topic['id']['seriesId'],
+ submissionId=submission['id']['submissionId'],
+ body=vote_body)
+ print "Voted on the submission"
+ except CredentialsInvalidError:
+ print 'Your credentials are no longer valid.'
+ print 'Please re-run this application to re-authorize.'
if __name__ == '__main__':
diff --git a/samples/moderator/three_legged_dance.py b/samples/moderator/three_legged_dance.py
deleted file mode 100644
index 32e624d..0000000
--- a/samples/moderator/three_legged_dance.py
+++ /dev/null
@@ -1,41 +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 1.0a three legged dance.
-
-Do the OAuth 1.0a three legged dance for
-a Buzz 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 apiclient.discovery import build
-from apiclient.oauth import FlowThreeLegged
-from apiclient.ext.authtools import run
-
-
-moderator_discovery = build("moderator", "v1").auth_discovery()
-
-flow = FlowThreeLegged(moderator_discovery,
- consumer_key='anonymous',
- consumer_secret='anonymous',
- user_agent='google-api-client-python-mdrtr-cmdline/1.0',
- domain='anonymous',
- scope='https://www.googleapis.com/auth/moderator',
- #scope='tag:google.com,2010:auth/moderator',
- xoauth_displayname='Google API Client Example App')
-
-run(flow, 'moderator.dat')
diff --git a/samples/threadqueue/main.py b/samples/threadqueue/main.py
index 4e5c511..d184f6d 100644
--- a/samples/threadqueue/main.py
+++ b/samples/threadqueue/main.py
@@ -1,9 +1,13 @@
from apiclient.discovery import build
-from apiclient.discovery import HttpError
+from apiclient.discovery import build
+from apiclient.errors import HttpError
+from apiclient.ext.authtools import run
+from apiclient.ext.file import Storage
+from apiclient.oauth import CredentialsInvalidError
+from apiclient.oauth import FlowThreeLegged
import Queue
import httplib2
-import pickle
import threading
import time
@@ -47,8 +51,9 @@
def process_requests():
http = httplib2.Http()
http = credentials.authorize(http)
+ credentials_ok = True
- while True:
+ while credentials_ok:
request = queue.get()
backoff = Backoff()
while backoff.loop():
@@ -59,10 +64,15 @@
if e.resp.status in [402, 403, 408, 503, 504]:
print "Increasing backoff, got status code: %d" % e.resp.status
backoff.fail()
+ except CredentialsInvalidError:
+ print "Credentials no long valid. Exiting."
+ credentials_ok = False
+ break
print "Completed request"
queue.task_done()
+
for i in range(NUM_THREADS):
t = threading.Thread(target=process_requests)
t.daemon = True
@@ -70,9 +80,20 @@
def main():
- f = open("moderator.dat", "r")
- credentials = pickle.loads(f.read())
- f.close()
+ storage = Storage('moderator.dat')
+ credentials = storage.get()
+ if credentials is None or credentials.invalid == True:
+ moderator_discovery = build("moderator", "v1").auth_discovery()
+
+ flow = FlowThreeLegged(moderator_discovery,
+ consumer_key='anonymous',
+ consumer_secret='anonymous',
+ user_agent='google-api-client-python-thread-sample/1.0',
+ domain='anonymous',
+ scope='https://www.googleapis.com/auth/moderator',
+ xoauth_displayname='Google API Client Example App')
+
+ credentials = run(flow, storage)
start_threads(credentials)
@@ -88,21 +109,26 @@
"videoSubmissionAllowed": False
}
}
- series = p.series().insert(body=series_body).execute()
- print "Created a new series"
+ try:
+ series = p.series().insert(body=series_body).execute()
+ print "Created a new series"
- for i in range(NUM_ITEMS):
- topic_body = {
- "data": {
- "description": "Sample Topic # %d" % i,
- "name": "Sample",
- "presenter": "me"
+ for i in range(NUM_ITEMS):
+ topic_body = {
+ "data": {
+ "description": "Sample Topic # %d" % i,
+ "name": "Sample",
+ "presenter": "me"
+ }
}
- }
- topic_request = p.topics().insert(seriesId=series['id']['seriesId'],
- body=topic_body)
- print "Adding request to queue"
- queue.put(topic_request)
+ topic_request = p.topics().insert(seriesId=series['id']['seriesId'],
+ body=topic_body)
+ print "Adding request to queue"
+ queue.put(topic_request)
+ except CredentialsInvalidError:
+ print 'Your credentials are no longer valid.'
+ print 'Please re-run this application to re-authorize.'
+
queue.join()
diff --git a/samples/threadqueue/three_legged_dance.py b/samples/threadqueue/three_legged_dance.py
deleted file mode 100644
index 12c495b..0000000
--- a/samples/threadqueue/three_legged_dance.py
+++ /dev/null
@@ -1,41 +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 1.0a three legged dance.
-
-Do the OAuth 1.0a three legged dance for
-a Buzz 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 apiclient.discovery import build
-from apiclient.oauth import FlowThreeLegged
-from apiclient.ext.authtools import run
-
-
-moderator_discovery = build("moderator", "v1").auth_discovery()
-
-flow = FlowThreeLegged(moderator_discovery,
- consumer_key='anonymous',
- consumer_secret='anonymous',
- user_agent='google-api-client-python-thread-sample/1.0',
- domain='anonymous',
- scope='https://www.googleapis.com/auth/moderator',
- #scope='tag:google.com,2010:auth/moderator',
- xoauth_displayname='Google API Client Example App')
-
-run(flow, 'moderator.dat')