Created
October 6, 2015 10:10
-
-
Save pellepim/665b46920aa8078b07cf to your computer and use it in GitHub Desktop.
>>> python oauth2client.py sessionName
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
from requests_oauthlib import OAuth2Session | |
import webbrowser | |
from time import sleep | |
import requests | |
from random import randint | |
import logging | |
import sys | |
logging.basicConfig(filename='oauth2.log', level=logging.INFO, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') | |
logger = logging.getLogger(__name__) | |
sessionName = sys.argv[1] | |
logging.info('starting session with name %s', sessionName) | |
# This information is obtained upon registration of a new GitHub | |
client_id = "REDACTED" | |
client_secret = "REDACTED" | |
authorization_base_url = 'https://api.projectplace.com/oauth2/authorize' | |
token_url = 'https://api.projectplace.com/oauth2/access_token' | |
api_endpoint = 'https://api.projectplace.com/1/user/me/profile' | |
token = None | |
verify_ssl = True | |
if token: | |
projectplace = OAuth2Session(client_id, token=token) | |
else: | |
projectplace = OAuth2Session(client_id) | |
if not token: | |
authorization_url, state = projectplace.authorization_url(authorization_base_url) | |
logging.info("%s: Redirecting to %s", sessionName, authorization_url) | |
webbrowser.open(authorization_url) | |
oauth_code = raw_input("Enter Code: ") | |
code = oauth_code | |
access_token_response = requests.post(token_url, data={ | |
'client_id': client_id, | |
'client_secret': client_secret, | |
'code': oauth_code, | |
'grant_type': 'authorization_code' | |
}) | |
if access_token_response.status_code == 200: | |
token = access_token_response.json() | |
logging.info('%s: User successfully authorized, with token: %s', sessionName, token) | |
projectplace = OAuth2Session(client_id=client_id, token=token) | |
while True: | |
response = projectplace.get(api_endpoint, verify=verify_ssl) | |
logging.info("%s: Calling with token %s ", sessionName, token['access_token']) | |
if response.status_code == 401 or randint(0, 5) == 1: | |
if response.status_code == 401: | |
logging.info('%s: Received 401 with response text: %s', sessionName, response.text) | |
else: | |
logging.info('%s: Refreshing token randomly, just to screw things up', sessionName) | |
refresh_response = requests.post(token_url, { | |
'client_id': client_id, | |
'client_secret': client_secret, | |
'refresh_token': token[u'refresh_token'], | |
'grant_type': 'refresh_token' | |
}, verify=verify_ssl) | |
if refresh_response.status_code == 200: | |
token = refresh_response.json() | |
projectplace = OAuth2Session(client_id=client_id, token=token) | |
logging.info('%s: Refreshing token worked, new access token: %s', sessionName, token) | |
if response.status_code == 200: | |
logging.info('%s: 200 OK Successfully fetched profile belonging to %s (%s)', sessionName, response.json()['sort_name'], response.json()['email']) | |
sleep(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment