Skip to content

Instantly share code, notes, and snippets.

@antimatter15
Created May 28, 2010 16:12
Show Gist options
  • Save antimatter15/417353 to your computer and use it in GitHub Desktop.
Save antimatter15/417353 to your computer and use it in GitHub Desktop.
import httplib
import time
import oauth
import urllib
from google.appengine.api import memcache
from django.utils import simplejson as json
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
# fake urls for the test server (matches ones in server.py)
REQUEST_TOKEN_URL = 'https://www.google.com/accounts/OAuthGetRequestToken'
ACCESS_TOKEN_URL = 'https://www.google.com/accounts/OAuthGetAccessToken'
AUTHORIZATION_URL = 'https://www.google.com/accounts/OAuthAuthorizeToken'
# key and secret granted by the service provider for this consumer application - same as the MockOAuthDataStore
CONSUMER_KEY = 'anonymous'
CONSUMER_SECRET = 'anonymous'
# example client using httplib with headers
class SimpleOAuthClient(oauth.OAuthClient):
def __init__(self, server, port=httplib.HTTP_PORT, request_token_url='', access_token_url='', authorization_url=''):
self.server = server
self.port = port
self.request_token_url = request_token_url
self.access_token_url = access_token_url
self.authorization_url = authorization_url
self.connection = httplib.HTTPSConnection('www.google.com')
def fetch_request_token(self, oauth_request):
# via headers
# -> OAuthToken
self.connection.request(oauth_request.http_method, self.request_token_url+'?'+urllib.urlencode(oauth_request.parameters))
response = self.connection.getresponse()
return oauth.OAuthToken.from_string(response.read())
def fetch_access_token(self, oauth_request):
# via headers
# -> OAuthToken
self.connection.request(oauth_request.http_method, self.access_token_url, headers=oauth_request.to_header())
response = self.connection.getresponse()
return oauth.OAuthToken.from_string(response.read())
def authorize_token(self, oauth_request):
# via url
# -> typically just some okay response
self.connection.request(oauth_request.http_method, oauth_request.to_url())
response = self.connection.getresponse()
return response.getheader('location')#.read()
class OAuthPartOne(webapp.RequestHandler):
def get(self):
client = SimpleOAuthClient("localhost", 80, REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, AUTHORIZATION_URL)
consumer = oauth.OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET)
signature_method_hmac_sha1 = oauth.OAuthSignatureMethod_HMAC_SHA1()
cid = self.request.get('waveid')
if cid is '':
self.response.out.write("ERROR: NO WAVE ID SPECIFIED")
return
callback = self.request.url[0:self.request.url.find('?')]+'2?id='+cid
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, callback=callback, http_url=client.request_token_url, parameters = {'scope':'http://wave.googleusercontent.com/api/rpc'})
oauth_request.sign_request(signature_method_hmac_sha1, consumer, None)
token = client.fetch_request_token(oauth_request)
memcache.set("PK_"+cid, token.to_string())
oauth_request = oauth.OAuthRequest.from_token_and_callback(token=token, http_url=client.authorization_url)
response = client.authorize_token(oauth_request)
self.redirect(response)
class OAuthPartTwo(webapp.RequestHandler):
def get(self):
verifier = self.request.get('oauth_verifier')
signature_method_hmac_sha1 = oauth.OAuthSignatureMethod_HMAC_SHA1()
tokenstr = memcache.get("PK_"+self.request.get('id'))
token = oauth.OAuthToken.from_string(tokenstr)
consumer = oauth.OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET)
client = SimpleOAuthClient("localhost", 80, REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, AUTHORIZATION_URL)
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=token, verifier=verifier, http_url=client.access_token_url)
oauth_request.sign_request(signature_method_hmac_sha1, consumer, token)
token = client.fetch_access_token(oauth_request)
cid = self.request.get('id')
memcache.set("AK_"+cid, token.to_string())
self.redirect('/banusers?id='+cid)
class BanUsers(webapp.RequestHandler):
def get(self):
tokenstr = memcache.get('AK_'+self.request.get('id'))
signature_method_hmac_sha1 = oauth.OAuthSignatureMethod_HMAC_SHA1()
token = oauth.OAuthToken.from_string(tokenstr)
consumer = oauth.OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET)
client = SimpleOAuthClient("localhost", 80, REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, AUTHORIZATION_URL)
RESOURCE = 'https://www-opensocial.googleusercontent.com/api/rpc'
def executeOperation(method, params):
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=token, http_method='POST', http_url=RESOURCE)
oauth_request.sign_request(signature_method_hmac_sha1, consumer, token)
body = json.dumps({'method': method, 'params': params})
#print oauth_request.to_url(), body
connection = httplib.HTTPSConnection('www-opensocial.googleusercontent.com')
connection.request("POST", oauth_request.to_url(), body, {'Content-type':'application/json'})
self.response.out.write(connection.getresponse().read())
waveId = 'googlewave.com!w+720bQAVlD'
waveletId = 'googlewave.com!conv+root'
participant = '[email protected]'
executeOperation('wave.wavelet.participant.add', {'waveletId': waveletId, 'waveId': waveId, 'participantId': participant})
executeOperation('wave.wavelet.modifyParticipantRole', {'waveletId': waveletId, 'waveId': waveId, 'participantId': participant, 'participantRole': "READ_ONLY"})
def main():
application = webapp.WSGIApplication([('/get_oauth_token', OAuthPartOne),
('/get_oauth_token2', OAuthPartTwo),
('/banusers', BanUsers)],
debug=True)
run_wsgi_app(application)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment