Skip to main content
Question

Getting a refresh token via Python Box SDK?

  • May 21, 2025
  • 4 replies
  • 41 views

Forum|alt.badge.img

Greetings, everyone:

 

A quick & simple question regarding the Python version of the Box SDK, and the various procedures needed for authentication.

 

Does the Box SDK provide an abstracted way for renewing the refresh token? Or must it always be done by making a POST request to https://api.box.com/oauth2/token ?

 

Thanks!

4 replies

Forum|alt.badge.img

Hi there,

No you don't have to explicitly call POST to refresh Token if you are using Python Box SDK. You do need to save it and read it from some persistent store though.

 Basicaly the oauth_class handles all the saving ,reading and refreshing of of tokens automatically. 

 

Here is the code snippet that you can refer to:

# coding: utf-8
from __future__ import print_function, unicode_literals
import os
from threading import Thread, Event
from boxsdk import OAuth2
import sys

 

#ClientID and Client_Secret of the Box application obtained from Box dev console
CLIENT_ID = ''
CLIENT_SECRET = ''


#authentication function
def authenticateAdmin(oauth_class=OAuth2):
    oauth = oauth_class(
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET,
    store_tokens=save_tokens,
)

#reading the tokens from the file
print("Reading tokens...")
os.system('cls')
at,rt = read_tokens_from_file()

oauth._access_token=at
oauth._refresh_token=rt
return oauth, at, rt

 

#save token to a file
def save_tokens(access_token,refresh_token):
    print("Refreshing tokens...")
    target = open(Utility.get_path() +"\\config\\AdminToken.txt", 'w')
    target.truncate()
    tokens = access_token+'#'+refresh_token
    target.write(tokens)
    target.close()

 

 

#reads tokens from a file
def read_tokens_from_file():
try:
    with open(Utility.get_path() + "\\config\\AdminToken.txt", 'r') as f:
        tokens=f.readline()
    return tokens.split('#')
except:
    print("Read token error:" + sys.exc_info())
    return "null","null"

 

as you can see there are function to save tokens (save_tokens()) and read tokens (read_tokens_from_file()). For instance i am saving it on config folder in AdminToken.txt in this instance.  When saving tokens i am saving it separating tokens with # and returning the tuple (accesstoken,refreshtoken) .

Let me know if this makes sense to you.

 

thanks, 

Bibek


Forum|alt.badge.img

Hello Bibek,

 

The script you pasted is for windows env ?

I hope this script supports OAuth2 , pls confirm ?

Where I can find AdminToken.txt ? 

does this works with Python2.7 ?

Working with Oauth2 requires browser interaction at-least once , does your script handle this ?


Forum|alt.badge.img

Hi can you please elaborate on the utility path and admintoken.txt how i have to give the path ... I tried giving the path but am not able to see any token or somthing storing there ..

if you dont mind can please share any of your social account to expalin my issue 

Thanks for the help.


Forum|alt.badge.img

Is there any reason why this is not the default behaviour?