Fix samples so that they catch AccessTokenRefreshError.
This commit is contained in:
@@ -54,9 +54,20 @@ for filename in glob.glob('samples/src/*.py'):
|
||||
for line in config.split('\n'):
|
||||
key, value = line[1:].split(':', 1)
|
||||
variables[key.strip()] = value.strip()
|
||||
|
||||
lines = content.split('\n')
|
||||
outlines = []
|
||||
for l in lines:
|
||||
if l:
|
||||
outlines.append(' ' + l)
|
||||
else:
|
||||
outlines.append('')
|
||||
content = '\n'.join(outlines)
|
||||
|
||||
variables['description'] = textwrap.fill(variables['description'])
|
||||
variables['content'] = content
|
||||
variables['name'] = os.path.basename(filename).split('.', 1)[0]
|
||||
|
||||
f = open(os.path.join('samples', variables['name'], variables['name'] + '.py'), 'w')
|
||||
f.write(template.substitute(variables))
|
||||
f.close()
|
||||
|
@@ -43,6 +43,7 @@ import sys
|
||||
|
||||
from apiclient.discovery import build
|
||||
from oauth2client.file import Storage
|
||||
from oauth2client.client import AccessTokenRefreshError
|
||||
from oauth2client.client import OAuth2WebServerFlow
|
||||
from oauth2client.tools import run
|
||||
|
||||
@@ -97,44 +98,49 @@ def main(argv):
|
||||
|
||||
service = build("buzz", "v1", http=http)
|
||||
|
||||
activities = service.activities()
|
||||
try:
|
||||
|
||||
# Retrieve the first two activities
|
||||
activitylist = activities.list(
|
||||
max_results='2', scope='@self', userId='@me').execute()
|
||||
print "Retrieved the first two activities"
|
||||
activities = service.activities()
|
||||
|
||||
# Retrieve the next two activities
|
||||
if activitylist:
|
||||
activitylist = activities.list_next(activitylist).execute()
|
||||
print "Retrieved the next two activities"
|
||||
# Retrieve the first two activities
|
||||
activitylist = activities.list(
|
||||
max_results='2', scope='@self', userId='@me').execute()
|
||||
print "Retrieved the first two activities"
|
||||
|
||||
# Add a new activity
|
||||
new_activity_body = {
|
||||
'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"
|
||||
# Retrieve the next two activities
|
||||
if activitylist:
|
||||
activitylist = activities.list_next(activitylist).execute()
|
||||
print "Retrieved the next two activities"
|
||||
|
||||
activitylist = activities.list(
|
||||
max_results='2', scope='@self', userId='@me').execute()
|
||||
# Add a new activity
|
||||
new_activity_body = {
|
||||
'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"
|
||||
|
||||
# Add a comment to that activity
|
||||
comment_body = {
|
||||
"content": "This is a comment"
|
||||
}
|
||||
item = activitylist['items'][0]
|
||||
comment = service.comments().insert(
|
||||
userId=item['actor']['id'], postId=item['id'], body=comment_body
|
||||
).execute()
|
||||
print 'Added a comment to the new activity'
|
||||
pprint.pprint(comment)
|
||||
activitylist = activities.list(
|
||||
max_results='2', scope='@self', userId='@me').execute()
|
||||
|
||||
# Add a comment to that activity
|
||||
comment_body = {
|
||||
"content": "This is a comment"
|
||||
}
|
||||
item = activitylist['items'][0]
|
||||
comment = service.comments().insert(
|
||||
userId=item['actor']['id'], postId=item['id'], body=comment_body
|
||||
).execute()
|
||||
print 'Added a comment to the new activity'
|
||||
pprint.pprint(comment)
|
||||
|
||||
|
||||
except AccessTokenRefreshError:
|
||||
print ("The credentials have been revoked or expired, please re-run"
|
||||
"the application to re-authorize")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(sys.argv)
|
||||
|
@@ -42,6 +42,7 @@ import sys
|
||||
|
||||
from apiclient.discovery import build
|
||||
from oauth2client.file import Storage
|
||||
from oauth2client.client import AccessTokenRefreshError
|
||||
from oauth2client.client import OAuth2WebServerFlow
|
||||
from oauth2client.tools import run
|
||||
|
||||
@@ -96,49 +97,54 @@ def main(argv):
|
||||
|
||||
service = build("moderator", "v1", http=http)
|
||||
|
||||
# Create a new Moderator series.
|
||||
series_body = {
|
||||
"description": "Share and rank tips for eating healthy and cheap!",
|
||||
"name": "Eating Healthy & Cheap",
|
||||
"videoSubmissionAllowed": False
|
||||
}
|
||||
series = service.series().insert(body=series_body).execute()
|
||||
print "Created a new series"
|
||||
try:
|
||||
|
||||
# Create a new Moderator topic in that series.
|
||||
topic_body = {
|
||||
"description": "Share your ideas on eating healthy!",
|
||||
"name": "Ideas",
|
||||
"presenter": "liz"
|
||||
}
|
||||
topic = service.topics().insert(seriesId=series['id']['seriesId'],
|
||||
body=topic_body).execute()
|
||||
print "Created a new topic"
|
||||
# Create a new Moderator series.
|
||||
series_body = {
|
||||
"description": "Share and rank tips for eating healthy and cheap!",
|
||||
"name": "Eating Healthy & Cheap",
|
||||
"videoSubmissionAllowed": False
|
||||
}
|
||||
series = service.series().insert(body=series_body).execute()
|
||||
print "Created a new series"
|
||||
|
||||
# Create a new Submission in that topic.
|
||||
submission_body = {
|
||||
"attachmentUrl": "http://www.youtube.com/watch?v=1a1wyc5Xxpg",
|
||||
"attribution": {
|
||||
"displayName": "Bashan",
|
||||
"location": "Bainbridge Island, WA"
|
||||
},
|
||||
"text": "Charlie Ayers @ Google"
|
||||
}
|
||||
submission = service.submissions().insert(seriesId=topic['id']['seriesId'],
|
||||
topicId=topic['id']['topicId'], body=submission_body).execute()
|
||||
print "Inserted a new submisson on the topic"
|
||||
# Create a new Moderator topic in that series.
|
||||
topic_body = {
|
||||
"description": "Share your ideas on eating healthy!",
|
||||
"name": "Ideas",
|
||||
"presenter": "liz"
|
||||
}
|
||||
topic = service.topics().insert(seriesId=series['id']['seriesId'],
|
||||
body=topic_body).execute()
|
||||
print "Created a new topic"
|
||||
|
||||
# Vote on that newly added Submission.
|
||||
vote_body = {
|
||||
"vote": "PLUS"
|
||||
}
|
||||
service.votes().insert(seriesId=topic['id']['seriesId'],
|
||||
submissionId=submission['id']['submissionId'],
|
||||
body=vote_body)
|
||||
print "Voted on the submission"
|
||||
# Create a new Submission in that topic.
|
||||
submission_body = {
|
||||
"attachmentUrl": "http://www.youtube.com/watch?v=1a1wyc5Xxpg",
|
||||
"attribution": {
|
||||
"displayName": "Bashan",
|
||||
"location": "Bainbridge Island, WA"
|
||||
},
|
||||
"text": "Charlie Ayers @ Google"
|
||||
}
|
||||
submission = service.submissions().insert(seriesId=topic['id']['seriesId'],
|
||||
topicId=topic['id']['topicId'], body=submission_body).execute()
|
||||
print "Inserted a new submisson on the topic"
|
||||
|
||||
# Vote on that newly added Submission.
|
||||
vote_body = {
|
||||
"vote": "PLUS"
|
||||
}
|
||||
service.votes().insert(seriesId=topic['id']['seriesId'],
|
||||
submissionId=submission['id']['submissionId'],
|
||||
body=vote_body)
|
||||
print "Voted on the submission"
|
||||
|
||||
|
||||
|
||||
except AccessTokenRefreshError:
|
||||
print ("The credentials have been revoked or expired, please re-run"
|
||||
"the application to re-authorize")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(sys.argv)
|
||||
|
@@ -43,6 +43,7 @@ import sys
|
||||
|
||||
from apiclient.discovery import build
|
||||
from oauth2client.file import Storage
|
||||
from oauth2client.client import AccessTokenRefreshError
|
||||
from oauth2client.client import OAuth2WebServerFlow
|
||||
from oauth2client.tools import run
|
||||
|
||||
@@ -97,34 +98,39 @@ def main(argv):
|
||||
|
||||
service = build("prediction", "v1.2", http=http)
|
||||
|
||||
# Name of Google Storage bucket/object that contains the training data
|
||||
OBJECT_NAME = "apiclient-prediction-sample/prediction_models/languages"
|
||||
try:
|
||||
|
||||
# Start training on a data set
|
||||
train = service.training()
|
||||
start = train.insert(data=OBJECT_NAME, body={}).execute()
|
||||
# Name of Google Storage bucket/object that contains the training data
|
||||
OBJECT_NAME = "apiclient-prediction-sample/prediction_models/languages"
|
||||
|
||||
print 'Started training'
|
||||
pprint.pprint(start)
|
||||
# Start training on a data set
|
||||
train = service.training()
|
||||
start = train.insert(data=OBJECT_NAME, body={}).execute()
|
||||
|
||||
import time
|
||||
# Wait for the training to complete
|
||||
while True:
|
||||
status = train.get(data=OBJECT_NAME).execute()
|
||||
pprint.pprint(status)
|
||||
if 'RUNNING' != status['trainingStatus']:
|
||||
break
|
||||
print 'Waiting for training to complete.'
|
||||
time.sleep(10)
|
||||
print 'Training is complete'
|
||||
print 'Started training'
|
||||
pprint.pprint(start)
|
||||
|
||||
# Now make a prediction using that training
|
||||
body = {'input': {'csvInstance': ["mucho bueno"]}}
|
||||
prediction = service.predict(body=body, data=OBJECT_NAME).execute()
|
||||
print 'The prediction is:'
|
||||
pprint.pprint(prediction)
|
||||
import time
|
||||
# Wait for the training to complete
|
||||
while True:
|
||||
status = train.get(data=OBJECT_NAME).execute()
|
||||
pprint.pprint(status)
|
||||
if 'RUNNING' != status['trainingStatus']:
|
||||
break
|
||||
print 'Waiting for training to complete.'
|
||||
time.sleep(10)
|
||||
print 'Training is complete'
|
||||
|
||||
# Now make a prediction using that training
|
||||
body = {'input': {'csvInstance': ["mucho bueno"]}}
|
||||
prediction = service.predict(body=body, data=OBJECT_NAME).execute()
|
||||
print 'The prediction is:'
|
||||
pprint.pprint(prediction)
|
||||
|
||||
|
||||
except AccessTokenRefreshError:
|
||||
print ("The credentials have been revoked or expired, please re-run"
|
||||
"the application to re-authorize")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(sys.argv)
|
||||
|
@@ -42,6 +42,7 @@ import sys
|
||||
|
||||
from apiclient.discovery import build
|
||||
from oauth2client.file import Storage
|
||||
from oauth2client.client import AccessTokenRefreshError
|
||||
from oauth2client.client import OAuth2WebServerFlow
|
||||
from oauth2client.tools import run
|
||||
|
||||
@@ -96,8 +97,13 @@ def main(argv):
|
||||
|
||||
service = build("$name", "$version", http=http)
|
||||
|
||||
try:
|
||||
|
||||
$content
|
||||
|
||||
except AccessTokenRefreshError:
|
||||
print ("The credentials have been revoked or expired, please re-run"
|
||||
"the application to re-authorize")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(sys.argv)
|
||||
|
@@ -43,6 +43,7 @@ import sys
|
||||
|
||||
from apiclient.discovery import build
|
||||
from oauth2client.file import Storage
|
||||
from oauth2client.client import AccessTokenRefreshError
|
||||
from oauth2client.client import OAuth2WebServerFlow
|
||||
from oauth2client.tools import run
|
||||
|
||||
@@ -97,20 +98,25 @@ def main(argv):
|
||||
|
||||
service = build("urlshortener", "v1", http=http)
|
||||
|
||||
url = service.url()
|
||||
try:
|
||||
|
||||
# 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)
|
||||
url = service.url()
|
||||
|
||||
short_url = resp['id']
|
||||
# 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)
|
||||
|
||||
# Convert the shortened URL back into a long URL
|
||||
resp = url.get(shortUrl=short_url).execute()
|
||||
pprint.pprint(resp)
|
||||
short_url = resp['id']
|
||||
|
||||
# Convert the shortened URL back into a long URL
|
||||
resp = url.get(shortUrl=short_url).execute()
|
||||
pprint.pprint(resp)
|
||||
|
||||
|
||||
except AccessTokenRefreshError:
|
||||
print ("The credentials have been revoked or expired, please re-run"
|
||||
"the application to re-authorize")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(sys.argv)
|
||||
|
Reference in New Issue
Block a user