Developer Community

Upcoming community maintenance Oct. 27th through Oct. 29th
For more info click here
Occasional Contributor I

Re: Python script to refresh access token

Ive managed to install the packages from the requirements.txt file. Im now looking to execute to see if it works. As per your comment, am I right in saying then that executing central_site_bringup will not create anything on Central at all? It is simply just there as a test in order to refresh the token? I am just a bit concerned of it creating sites etc. that I do not need to create.


As well as that, you mentioned just calling the By doing this, would it just run the refresh script and store it in the file that is mentioned? I presume I would go about this by doing "python3 -i=input_info.json". 


Only reason I ask is that this script is only to be used to refresh the token so that it can be used with other scripts to pull Rest API Data down for a dashboard. 

Contributor I

Re: Python script to refresh access token

Hi James,


Not sure where you're at with this issue but I have a few remarks that might help.


First of all, I think you've got it wrong on this line:

refresh_token = open('C:\Users\james.weston1\Desktop\access_token.json')


This returns a file pointer, not a string. Moreover, if the file contents is actually JSON, you would have to parse it to retrieve the token value.


So assuming the file contents looks like this:

{"refresh_token": "123456"}


Your script should look more like this:

import requests
import json

client_id = 123
client_secret = 123
url = ""

with open('C:\Users\james.weston1\Desktop\access_token.json') as f:
    # Parse the file contents as json
    access_data = json.load(f)
    # Get the refresh token from the resulting dict
    refresh_token = access_data['refresh_token']

qparams = {
    "client_id": client_id,
    "client_secret": client_secret,
    "refresh_token": refresh_token

response = requests.request("POST", url, params=qparams)



Then if you want to store the new refresh token into the same file, you would do something like this (in the same script):


response = requests.request("POST", url, params=qparams)

# extract the new refresh oken from the response
new_refresh_token = response.json()['resfresh_token']

# Update the access data you previously got from your json file
access_data['refresh_token'] = new_refresh_token

# Write the data back to your json file
with open('your/file.json', 'w') as f:
   json.dump(access_data, f)


Hope it helps.

Search Airheads
Showing results for 
Search instead for 
Did you mean: