How to authenticate using okta sso for a python script?




  • jmoldow_box



    You cannot immediately use the Python SDK out-of-the-box. It will only work in the context of an application. You'll need to go to to create one, if you don't already have one.


    If this is a one-time or infrequent thing you plan to do, you can click the "Edit Application" button next to your application, and then click the "Create a developer token" button, and copy the token it generates. Then, in Python, you can do


    import boxsdk

    client = boxsdk.DeveloperTokenClient()


    After that, you'll be prompted for the token, and you'll need to paste it into your terminal and press Enter. After that, you can make all the API calls you need, until the token expires in an hour.


    If you plan to do this more frequently, or plan to distribute your application to other users, then you will need to prompt for authorization. If this app is living on your own computer (which is what it sounds like), you can spin off a Python thread to run a temporary webserver on some port on localhost. In your application configuration at, you'll need to configure this as your app's redirect_uri. Then you'll need to configure the SDK with your client_id, client_secret, and redirect_uri. When the webserver receives the temporary auth code, it should call OAuth2.authenticate(auth_code) to complete the authentication process.


    For more info, see , and a demo of this strategy at .

    コメントアクション Permalink
  • SalsaShark42

    I have a utility that does something similar to what you're describing, though in a different context.


    I used the developer tokens while I was doing my initial testing on my Box developer instance.


    Then I switched to the access/refresh token approach, using the 'keyring' module to store the tokens in between script runs.  In this case, you only need to do the authorization one time as long as your script runs at least once every 60 days.

    コメントアクション Permalink
  • haixu

    The environment that I run the python script in is an embedded system with limited resource so having some sort of server to help with the authentication does not work too well.  So I guess I am looking for a way to access box via scripting without having to emulate a web application or web intergration.  Is there a possible solution for this?

    I have a process that monitor a certain event on the embedded device.  When that event happened, I want it to kick off a script that will upload a certain set of files to box.  The current scheme of authentication is that I have to have the access token and refresh token available when that event happened.  This mean I have to have another process that must keep the access token available all the time without expiring.  It will work but it is adding extra overhead with the limited resource that I have.  I am looking for a better solution if there is one.

    コメントアクション Permalink