Welcome to the new Box Support website. Check out all the details here on what’s changed.

Getting a refresh token via Python Box SDK?

New post

Comments

4 comments

  • Bibek

    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

    0
    Comment actions Permalink
  • bhanuprakashemm

    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 ?

    0
    Comment actions Permalink
  • jamreddy

    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.

    0
    Comment actions Permalink
  • Eduardo Pignatelli

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

    0
    Comment actions Permalink

Please sign in to leave a comment.