Developer Community

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

Python script to refresh access token

Hi,

 

Im trying to convert a bash script into python in order to refresh the access tokens which are used by Aruba Central for the Rest API.

 

The current bash script is as follows,

 

Spoiler

client_id=123
client_secret=123
access_token="\Documents\Tokens\access_token.json"

REFRESH_TOKEN=$ cat $access_token.json | jq -r '.refresh_token')

curl https://eu-apigw.central.arubanetworks.com/oauth2/token -d "client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&grant_type=refresh_token&refresh_token=$REFRESH_TOKEN" > $access_token

 

 

Ive managed to convert the curl into the following python, but I am not sure if this is correct or not.

 

Spoiler

data = {
'client_id': '$CLIENT_ID',
'client_secret': '$CLIENT_SECRET',
'grant_type': 'refresh_token',
'refresh_token': '$REFRESH_TOKEN'
}

response = requests.post('https://eu-apigw.central.arubanetworks.com/oauth2/token?client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&grant_type=refresh_token&refresh_token=$REFRESH_TOKEN', data=data)

The aim for this is to have the script send out a request for a refresh of the token by inputting the Client ID, Client Secret, and Refresh Token (found in the access_token.json file) into the URL, sending the request, and putting the data it receives back into the access_token.json file. 

 

This script worked on a Linux environment, but it needs to be converted to work in windows and I am struggling to do so, so any help would be much appreciated.

 

Highlighted
Aruba Employee

Re: Python script to refresh access token

Hi James,

For your reference please find an exmaple below:

*******************************************************************

import requests


url = "https://eu-apigw.central.arubanetworks.com/oauth2/token"

qparams = {"client_id": "$CLIENT_ID", "client_secret": "$CLIENT_SECRET", "grant_type":"refresh_token", "refresh_token":"$REFRESH_TOKEN"}

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

print(response.text.encode('utf8'))
*******************************************************************

The above example works fine for me. Let me know if I can help you with anything else.

 

Regards,

Jay

 

Highlighted
Occasional Contributor I

Re: Python script to refresh access token

Hi Jay,

 

Thank you for getting back to me regarding this. Ive put in the code you have attached but I am still struggling to get it to refresh for some reason. Im not sure if it is not working as a result of me trying to copy the refresh_token from another file or not? As well as this, how would I go about copying the new token data from the new request into that same access_token.json file if it is at all possible? I hope this all makes sense? I appreciate the help so far, thank you. Ive attached the new code below:

*******************************************************************

import requests

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

url = "https://eu-apigw.central.arubanetworks.com/oauth2/token"

qparams = {"client_id": "$CLIENT_ID", "client_secret": "$CLIENT_SECRET", "grant_type":"refresh_token", "refresh_token":"$REFRESH_TOKEN"}

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

print(response.text.encode('utf8'))

*******************************************************************

Highlighted
Occasional Contributor I

Re: Python script to refresh access token

Hi Jay,

 

Thank you for getting back to me regarding this. Ive put in the code you have attached but I am still struggling to get it to refresh for some reason. Im not sure if it is not working as a result of me trying to copy the refresh_token from another file or not? As well as this, how would I go about copying the new token data from the new request into that same access_token.json file if it is at all possible? I hope this all makes sense? I appreciate the help so far, thank you. Ive attached the new code below:

*******************************************************************

import requests

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

url = "https://eu-apigw.central.arubanetworks.com/oauth2/token"

qparams = {"client_id": "$CLIENT_ID", "client_secret": "$CLIENT_SECRET", "grant_type":"refresh_token", "refresh_token":"$REFRESH_TOKEN"}

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

print(response.text.encode('utf8'))

*******************************************************************

Highlighted
Occasional Contributor I

Re: Python script to refresh access token

Hi Jay,

 

Thank you for getting back to me regarding this. Ive put in the code you have attached but I am still struggling to get it to refresh for some reason. Im not sure if it is not working as a result of me trying to copy the refresh_token from another file or not? As well as this, how would I go about copying the new token data from the new request into that same access_token.json file if it is at all possible? I hope this all makes sense? I appreciate the help so far, thank you. Ive attached the new code below:

*******************************************************************

import requests

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

url = "https://eu-apigw.central.arubanetworks.com/oauth2/token"

qparams = {"client_id": "$CLIENT_ID", "client_secret": "$CLIENT_SECRET", "grant_type":"refresh_token", "refresh_token":"$REFRESH_TOKEN"}

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

print(response.text.encode('utf8'))

*******************************************************************

Highlighted
Aruba Employee

Re: Python script to refresh access token

Hi James,

 

In addition to Jay's response,

 

You could refer the following python sample. It has the following functions

  • To generate new access token by OAUTH authentication
  • Refresh Token based on expired/current token
  • store/overwrite current/refreshed token in a pickle file.
  • Load token from the pickle file
  • Do everything in one function. That is, if pickle file was available and it will refresh the existing token. (OR) it will create a new access token. Either way, the new token will be stored in the same pickle file

Git Repo: https://github.com/aruba/central-examples-only/tree/master/rest-api-python-scripts

 

File in discussion:

https://github.com/aruba/central-examples-only/blob/master/rest-api-python-scripts/central_session.py

 

Hope this helps!

 

Regards,

Karthik

Highlighted
Occasional Contributor I

Re: Python script to refresh access token

Hi Both,

 

Ive tried both solutions but im still failing to generate a new token each time I run the python script.

 

Jay, I used your response and edited the code as follows

*******************************************************************

import requests

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

url = "https://eu-apigw.central.arubanetworks.com/oauth2/token"

qparams = {"client_id": "$CLIENT_ID", "client_secret": "$CLIENT_SECRET", "grant_type":"refresh_token", "refresh_token":"$REFRESH_TOKEN"}

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

print(response.text.encode('utf8'))

*******************************************************************

Im not sure if it is not working as a result of me trying to copy the refresh_token from another file or not. Im also unsure if this code is saving the new token to the acces_token.json file or not where im trying to retrieve it from if this makes sense?

 

Karthik, Ive also downloaded the Git you have linked and the input_info.json file. Ive inputted my credentials into input_info and when looking through central_session it didnt appear like I needed to change anything so I left that as it was. However, when trying to run this it also fails to refresh the token too and just closes. Im not sure if there is anything else I need to be editing?

 

Im checking the refresh by downloading my token and seeing if it changes so I hope this is the correct process to check?

 

Many thanks to you both for looking into this for me.

Aruba Employee

Re: Python script to refresh access token

If you had executed the script based on the readme section with this command,

 

python3 central_site_bringup.py -i=input_info.json

 

it would have created a file under script directory, temp/tok_<customer_id>.pickle. If that file was already there, it would have refreshed the token in the file. It will not be displayed in the screen as output though. You could also check by downloading the token in the UI. The purpose of the file is to act as a sample for creating groups, sites and templates (this piece of code is commented out in the script).

 

You could do more based on other files in the repo.

 

Highlighted
Occasional Contributor I

Re: Python script to refresh access token

It turns out I didnt install the requirements so thats my apologies. However, im struggling with this part too im afraid. When using the command pip3 install -r requirements.txt in cmd/python it just spits out errors of invalid syntax etc. 

 

Alongside this, when I get it working, do I need to execute the central_site_bringup.py as if possible id like not to create a new site. I know it deletes it straight after but id like to try and avoid this either way if possible just incase.

 

Sorry for all the trouble. Many thanks.

Highlighted
Aruba Employee

Re: Python script to refresh access token

You could install packages mentioned in the requirements.txt by yourself as well. I prefer, creating a virtual environment.

 

You could directly make a call to the functions available in central_session.py. In case, using the central_site_bringup.py also works. It does not create/delete site since all the code is commented out for reference purpose.

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