Skip to content

Instantly share code, notes, and snippets.

@awans
Created January 30, 2011 21:03
Show Gist options
  • Save awans/803249 to your computer and use it in GitHub Desktop.
Save awans/803249 to your computer and use it in GitHub Desktop.
Complete implementation of OAuth2 (the hard version) in about 10 lines of code
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.api import urlfetch
from django.utils import simplejson as json
import urllib
OAUTH_CLIENT_ID = "1027534278801.apps.googleusercontent.com"
OAUTH_CLIENT_SECRET = "X0ezwreyuXIzNx9vDUkUYmDZ"
class MainHandler(webapp.RequestHandler):
def get(self):
params = urllib.urlencode({
"response_type": "code",
"redirect_uri": "http://localhost:8081/oa2cb",
"client_id":OAUTH_CLIENT_ID,
"scope":"https://www.googleapis.com/auth/buzz"
})
url = "https://www.google.com/accounts/o8/oauth2/authorization?%s" % params
self.response.out.write('<a href="%s">oauth!</a>' % url)
class OAuthHandler(webapp.RequestHandler):
def get(self):
payload = urllib.urlencode({
"code": self.request.get('code'),
"grant_type": "authorization_code",
"redirect_uri": "http://localhost:8081/oa2cb",
"client_id":OAUTH_CLIENT_ID,
"client_secret":OAUTH_CLIENT_SECRET
})
url = "https://www.google.com/accounts/o8/oauth2/token"
res_str = urlfetch.fetch(url, payload=payload, method=urlfetch.POST)
obj = json.loads(res_str.content)
access = obj["access_token"]
self.response.out.write(obj)
def main():
application = webapp.WSGIApplication([('/', MainHandler),
('/oa2cb', OAuthHandler)],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment