Created
November 8, 2014 13:24
-
-
Save mrfuxi/e6c009dd85b38796a858 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import requests | |
import urllib | |
import time | |
import webbrowser | |
import json | |
AUTH_JSON = { | |
"installed": { | |
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | |
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | |
"client_email": "", | |
"client_id": "ABCDEF.apps.googleusercontent.com", | |
"client_secret": "some_secret_key", | |
"client_x509_cert_url": "", | |
"redirect_uris": [ | |
"urn:ietf:wg:oauth:2.0:oob", | |
"oob" | |
], | |
"token_uri": "https://accounts.google.com/o/oauth2/token" | |
} | |
} | |
AUTH_URI = AUTH_JSON["installed"]["auth_uri"] | |
TOKEN_URI = AUTH_JSON["installed"]["token_uri"] | |
CLIENT_ID = AUTH_JSON["installed"]["client_id"] | |
CLIENT_SECRET = AUTH_JSON["installed"]["client_secret"] | |
REDIRECT_URI = AUTH_JSON["installed"]["redirect_uris"][0] | |
link_payload = { | |
"client_id": CLIENT_ID, | |
"redirect_uri": REDIRECT_URI, | |
"scope": "openid https://www.googleapis.com/auth/plus.login", | |
"response_type": "code", | |
} | |
link_query = urllib.urlencode(link_payload) | |
link_url = "%s?%s" % (AUTH_URI, link_query) | |
webbrowser.open(link_url) | |
time.sleep(0.5) | |
auth_code = raw_input("Code:") | |
#Refreshed Token | |
refresh_payload = { | |
"code": auth_code, | |
"client_id": CLIENT_ID, | |
"client_secret": CLIENT_SECRET, | |
"redirect_uri": REDIRECT_URI, | |
"grant_type": "authorization_code", | |
} | |
req = requests.post(TOKEN_URI, data=refresh_payload) | |
refresh_token_data = json.loads(req.content) | |
REFRESH_TOKEN = refresh_token_data["refresh_token"] | |
print "Refreshed token:", REFRESH_TOKEN | |
final_token_payload = { | |
"client_id": CLIENT_ID, | |
"client_secret": CLIENT_SECRET, | |
"refresh_token": REFRESH_TOKEN, | |
"grant_type": "refresh_token", | |
} | |
req = requests.post(TOKEN_URI, data=final_token_payload) | |
final_token_data = json.loads(req.content) | |
print final_token_data | |
app_req = requests.get( | |
"http://my-app.appspot.com/api/", | |
headers={ | |
"Content-Type": "application/x-www-form-urlencoded", | |
"Authorization": "Bearer %s" % final_token_data["access_token"], | |
} | |
) | |
print app_req.content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment