Moving all OAuth code and samples to use Storage consistently
This commit is contained in:
4
Makefile
4
Makefile
@@ -3,3 +3,7 @@ pep8:
|
||||
|
||||
test:
|
||||
python runtests.py tests
|
||||
|
||||
.PHONY: docs
|
||||
docs:
|
||||
cd docs; ./build.sh
|
||||
|
||||
@@ -157,6 +157,11 @@ Args:<br>
|
||||
This is needed to store the latest access_token if it<br>
|
||||
has expired and been refreshed.</tt></dd></dl>
|
||||
|
||||
<hr>
|
||||
Data descriptors inherited from <a href="oauth2client.client.html#OAuth2Credentials">OAuth2Credentials</a>:<br>
|
||||
<dl><dt><strong>invalid</strong></dt>
|
||||
<dd><tt>True if the credentials are invalid, such as being revoked.</tt></dd>
|
||||
</dl>
|
||||
<hr>
|
||||
Data descriptors inherited from <a href="oauth2client.client.html#Credentials">Credentials</a>:<br>
|
||||
<dl><dt><strong>__dict__</strong></dt>
|
||||
@@ -406,6 +411,11 @@ Args:<br>
|
||||
This is needed to store the latest access_token if it<br>
|
||||
has expired and been refreshed.</tt></dd></dl>
|
||||
|
||||
<hr>
|
||||
Data descriptors defined here:<br>
|
||||
<dl><dt><strong>invalid</strong></dt>
|
||||
<dd><tt>True if the credentials are invalid, such as being revoked.</tt></dd>
|
||||
</dl>
|
||||
<hr>
|
||||
Data descriptors inherited from <a href="oauth2client.client.html#Credentials">Credentials</a>:<br>
|
||||
<dl><dt><strong>__dict__</strong></dt>
|
||||
|
||||
@@ -139,7 +139,12 @@ class OAuth2Credentials(Credentials):
|
||||
|
||||
# True if the credentials have been revoked or expired and can't be
|
||||
# refreshed.
|
||||
self.invalid = False
|
||||
self._invalid = False
|
||||
|
||||
@property
|
||||
def invalid(self):
|
||||
"""True if the credentials are invalid, such as being revoked."""
|
||||
return self._invalid
|
||||
|
||||
def set_store(self, store):
|
||||
"""Set the storage for the credential.
|
||||
@@ -202,7 +207,7 @@ class OAuth2Credentials(Credentials):
|
||||
try:
|
||||
d = simplejson.loads(content)
|
||||
if 'error' in d:
|
||||
self.invalid = True
|
||||
self._invalid = True
|
||||
self.store(self)
|
||||
except:
|
||||
pass
|
||||
@@ -272,7 +277,7 @@ class AccessTokenCredentials(OAuth2Credentials):
|
||||
only the access_token is present it can not be refreshed and will in time
|
||||
expire.
|
||||
|
||||
OAuth2Credentials objects may be safely pickled and unpickled.
|
||||
AccessTokenCredentials objects may be safely pickled and unpickled.
|
||||
|
||||
Usage:
|
||||
credentials = AccessTokenCredentials('<an access token>',
|
||||
|
||||
@@ -35,7 +35,7 @@ def main():
|
||||
flow = FlowThreeLegged(buzz_discovery,
|
||||
consumer_key='anonymous',
|
||||
consumer_secret='anonymous',
|
||||
user_agent='google-api-client-python-buzz-cmdline/1.0',
|
||||
user_agent='python-buzz-sample/1.0',
|
||||
domain='anonymous',
|
||||
scope='https://www.googleapis.com/auth/buzz',
|
||||
xoauth_displayname='Google API Client Example App')
|
||||
|
||||
@@ -17,14 +17,35 @@ from apiclient.discovery import build
|
||||
import httplib2
|
||||
import pickle
|
||||
|
||||
from apiclient.discovery import build
|
||||
from apiclient.oauth import FlowThreeLegged
|
||||
from apiclient.ext.authtools import run
|
||||
from apiclient.ext.file import Storage
|
||||
|
||||
# Uncomment to get detailed logging
|
||||
# httplib2.debuglevel = 4
|
||||
|
||||
|
||||
def main():
|
||||
f = open("latitude.dat", "r")
|
||||
credentials = pickle.loads(f.read())
|
||||
f.close()
|
||||
credentials = Storage('latitude.dat').get()
|
||||
if credentials is None:
|
||||
auth_discovery = build("latitude", "v1").auth_discovery()
|
||||
flow = FlowThreeLegged(auth_discovery,
|
||||
# You MUST have a consumer key and secret tied to a
|
||||
# registered domain to use the latitude API.
|
||||
#
|
||||
# https://www.google.com/accounts/ManageDomains
|
||||
consumer_key='REGISTERED DOMAIN NAME',
|
||||
consumer_secret='KEY GIVEN DURING REGISTRATION',
|
||||
user_agent='google-api-client-python-latitude/1.0',
|
||||
domain='REGISTERED DOMAIN NAME',
|
||||
scope='https://www.googleapis.com/auth/latitude',
|
||||
xoauth_displayname='Google API Latitude Example',
|
||||
location='current',
|
||||
granularity='city'
|
||||
)
|
||||
|
||||
credentials = run(flow, 'latitude.dat')
|
||||
|
||||
http = httplib2.Http()
|
||||
http = credentials.authorize(http)
|
||||
|
||||
@@ -1,46 +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("latitude", "v1").auth_discovery()
|
||||
|
||||
flow = FlowThreeLegged(moderator_discovery,
|
||||
# You MUST have a consumer key and secret tied to a
|
||||
# registered domain to use the latitude API.
|
||||
#
|
||||
# https://www.google.com/accounts/ManageDomains
|
||||
consumer_key='REGISTERED DOMAIN NAME',
|
||||
consumer_secret='KEY GIVEN DURING REGISTRATION',
|
||||
user_agent='google-api-client-python-latitude/1.0',
|
||||
domain='REGISTERED DOMAIN NAME',
|
||||
scope='https://www.googleapis.com/auth/latitude',
|
||||
xoauth_displayname='Google API Latitude Example',
|
||||
location='current',
|
||||
granularity='city'
|
||||
)
|
||||
|
||||
run(flow, 'latitude.dat')
|
||||
@@ -48,21 +48,7 @@ class MainHandler(webapp.RequestHandler):
|
||||
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 @@ class MainHandler(webapp.RequestHandler):
|
||||
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):
|
||||
|
||||
@@ -27,7 +27,7 @@ from oauth2client.tools import run
|
||||
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',
|
||||
|
||||
@@ -22,7 +22,7 @@ STEP2_URI = 'http://gregorio2.cnc.corp.google.com:8000/auth_return'
|
||||
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 @@ def index(request):
|
||||
'activitylist': activitylist,
|
||||
})
|
||||
|
||||
|
||||
@login_required
|
||||
def auth_return(request):
|
||||
try:
|
||||
|
||||
@@ -26,7 +26,7 @@ def main():
|
||||
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',
|
||||
|
||||
@@ -26,7 +26,7 @@ def main():
|
||||
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',
|
||||
|
||||
@@ -37,26 +37,6 @@ def main():
|
||||
|
||||
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 @@ def main():
|
||||
pprint.pprint(resp)
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user