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