Skip to content

Instantly share code, notes, and snippets.

@yamatt
Created April 11, 2012 09:56
Show Gist options
  • Save yamatt/2358321 to your computer and use it in GitHub Desktop.
Save yamatt/2358321 to your computer and use it in GitHub Desktop.
Teaching myself the Google OAuth process
from urllib import urlencode
from urllib2 import Request, urlopen, HTTPError
from json import loads as jsonsload
dictionary_of_useful_things = {
"client_id": "261777520064-etb9m51k800e6ucrcf767th54uc5nt7i.apps.googleusercontent.com",
"client_secret": "{{ it's a secret. SSHHHhhh! }}",
"redirect_uri": "urn:ietf:wg:oauth:2.0:oob",
"grant_type": "authorization_code"
}
authorisation_request_url = r"https://accounts.google.com/o/oauth2/auth?scope=https%%3A%%2F%%2Fwww.googleapis.com%%2Fauth%%2Fplus.me&redirect_uri=%(redirect_uri)s&response_type=code&client_id=%(client_id)s" % dictionary_of_useful_things
print "Go to this url to allow the application access to your Google account: %s" % authorisation_request_url
dictionary_of_useful_things['code'] = raw_input("Paste the authorisation code here: ")
print "Thanks."
token_request_url = "https://accounts.google.com/o/oauth2/token"
encoded_token_request_params = urlencode(dictionary_of_useful_things)
request = Request(token_request_url, encoded_token_request_params)
try:
response = urlopen(request)
print "Success!"
token = jsonsload(response.read())
print token
except HTTPError, e:
print "The page load failed with this error code %d because of this bloomin error: %s" % (e.code, e.read())
@yamatt
Copy link
Author

yamatt commented Apr 11, 2012

Really needs some tidying

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment