If I understand correctly this should be what you are looking for:
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run_flow
from oauth2client.file import Storage
import requests
import os
CLIENT_ID = '9453asfasfaksdfh860b1osoiveogstt.apps.googleusercontent.com'
CLIENT_SECRET = '6gRid8wF7TW8asdfasdftX'
def get_new_token():
flow = OAuth2WebServerFlow(client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
scope='https://www.googleapis.com/auth/admin.directory.user https://www.googleapis.com/auth/admin.directory.user.readonly',
redirect_uri='http://example.com/auth_return')
storage = Storage('creds.data') #token details stored here
credentials = run_flow(flow, storage)
tokenhere=credentials.access_token #tokens generated
return tokenhere
def retrieve_saved_token():
if os.path.exists('creds.data'):
with open('creds.data') as creds:
tokenhere = creds.read() # Change to reflect how the token data is reflected in your 'creds.data' file
return tokenhere
def request_page(tokenhere):
r = requests.get("https://www.googleapis.com/admin/directory/v1/users?domain=mydomain.com",
headers={'Authorization': 'Bearer {token}'.format(token=tokenhere)})
result = r.json()
try:
tokenhere = retrieve_saved_token()
request_page(tokenhere)
except:
tokenhere = get_new_token()
request_page(tokenhere)
Broken down here are all the components of what was done
Just to be more object oriented I moved your logic into functions:
def get_new_token():
flow = OAuth2WebServerFlow(client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
scope='https://www.googleapis.com/auth/admin.directory.user https://www.googleapis.com/auth/admin.directory.user.readonly',
redirect_uri='http://example.com/auth_return')
storage = Storage('creds.data') #token details stored here
credentials = run_flow(flow, storage)
tokenhere=credentials.access_token #tokens generated
return tokenhere
def request_page(tokenhere):
r = requests.get("https://www.googleapis.com/admin/directory/v1/users?domain=mydomain.com",
headers={'Authorization': 'Bearer {token}'.format(token=tokenhere)})
result = r.json()
I also added a new function to retrieve a saved token if the token file exists:
def retrieve_saved_token():
if os.path.exists('creds.data'):
with open('creds.data') as creds:
tokenhere = creds.read() # Change to reflect how the token data is reflected in your 'creds.data' file
return tokenhere
Finally we get to the part where the logic actually runs.
First it tries to retrieve the token from the file, if the file doesn't exist then an exception would be raised and you would get to the exception logic.
The way the try logic is written will also catch if it is successful in retrieving a token but if that token is expired then the request logic should raise an exception.
In the Exception logic it does your original logic of getting a new token and requesting the page with that token
try:
tokenhere = retrieve_saved_token()
request_page(tokenhere)
except:
tokenhere = get_new_token()
request_page(tokenhere)
EDIT 1
I am assuming that you don't just want to request the page but also manipulate the data on the page. To do that you would have another function that pulls data off the page by looking for tags in the json object 'result' that you created. I call this function 'do_something_with_request_function()' in the code below.
The first Try/Except will check if a token file exists, if it does then grab that token and use it, if it doesn't then it will generate a new token.
The second try except has a chance of passing in an expired token. So lets assume two scenarios.
Scenario 1: you pass in a valid token, you get the page you need in the request. Your method that you use to parse the data on the page will look for specific fields that exist on that page, everything works fine.
Scenario 2: you pass an invalid token, you get an error page for expired/invalid token. Your method that parses that page to json and tries to manipulate the data would throw errors if it didn't find a value that would normally exist on the actual page you are trying to get to. This would force the code to jump to the except block.
The except block generates a new token, passes that in to request the page again and will process your data properly.
try:
tokenhere = retrieve_saved_token()
except:
tokenhere = get_new_token()
try:
request_page(tokenhere)
do_something_with_request_function(result)
except:
tokenhere = get_new_token()
request_page(tokenhere)
do_something_with_request_function(result)