Developer

last person joined: 7 days ago 

Expand all | Collapse all

Python script to refresh access token

This thread has been viewed 4 times
  • 1.  Python script to refresh access token

    Posted Feb 26, 2020 09:23 AM

    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.

     



  • 2.  RE: Python script to refresh access token

    EMPLOYEE
    Posted Feb 26, 2020 03:43 PM

    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

     



  • 3.  RE: Python script to refresh access token

    Posted Feb 27, 2020 02:32 AM

    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'))

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



  • 4.  RE: Python script to refresh access token

    Posted Feb 27, 2020 02:39 AM

    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'))

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



  • 5.  RE: Python script to refresh access token

    Posted Feb 27, 2020 02:40 AM

    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'))

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



  • 6.  RE: Python script to refresh access token

    EMPLOYEE
    Posted Feb 27, 2020 02:45 AM

    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



  • 7.  RE: Python script to refresh access token

    Posted Feb 27, 2020 03:09 AM

    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.



  • 8.  RE: Python script to refresh access token

    EMPLOYEE
    Posted Feb 27, 2020 03:21 AM

    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.

     



  • 9.  RE: Python script to refresh access token

    Posted Feb 27, 2020 03:43 AM

    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.



  • 10.  RE: Python script to refresh access token

    EMPLOYEE
    Posted Feb 27, 2020 03:50 AM

    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.



  • 11.  RE: Python script to refresh access token

    Posted Feb 27, 2020 04:21 AM

    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 central_session.py. 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 central_session.py -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. 



  • 12.  RE: Python script to refresh access token

    Posted Mar 13, 2020 10:58 AM

    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 = "https://eu-apigw.central.arubanetworks.com/oauth2/token"
    
    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 = {
        "grant_type":"refresh_token",
        "client_id": client_id,
        "client_secret": client_secret,
        "refresh_token": refresh_token
    }
    
    response = requests.request("POST", url, params=qparams)
    
    print(response.text.encode('utf8'))

     

    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.