Skip to main content
Question

Python - Which auth method to use if i want to run the python script to move the files within box.

  • May 22, 2025
  • 4 replies
  • 40 views

Forum|alt.badge.img

Which auth method should i use if i only want to work with the files(move/upload/generate link/delete.. etc) within my box account. 

4 replies

Forum|alt.badge.img

You can use OAuth 2 implementation for that - when you go through the auth process it will create a token to work with content in whatever account you log into. The one caveat here is that when using this method you will be forwarded to Box to log in and grant permissions for the application.

 

To avoid that, you can use the JWT implementation and then create a user token to upload to a specific user in your enterprise. Here are some guides on that:

- Jon


Forum|alt.badge.img

I am getting below error in python while trying steps - https://github.com/box/box-python-sdk 

https://developer.box.com/docs/authenticate-with-oauth-2

 

BoxOAuthException: 
Message: b'{"error":"invalid_grant","error_description":"Auth code doesn\'t exist or is invalid for the client"}'
Status: 400
URL: https://api.box.com/oauth2/token
Method: POST

 


Forum|alt.badge.img

 can you please add your code for the application? Alternatively I have this sample OAuth 2 Python application that uses Flask: https://github.com/jcleblanc/box-examples/blob/master/python/samples/auth_oauth.py. The config file for that example is here: https://github.com/jcleblanc/box-examples/blob/master/python/samples/config_oauth.py.


Forum|alt.badge.img

Below is the code snapshot

 

from boxsdk import Client
from boxsdk import OAuth2

# Auth config
client_id = 'my_client_ID'
client_secret='my_client_secret'
redirect_uri = 'https://app.box.com'

# Create new OAuth client & csrf token
oauth = OAuth2(
client_id=client_id,
client_secret=client_secret
)
auth_url, csrf_token = oauth.get_authorization_url(redirect_uri)
print(auth_url)
print(csrf_token)

 

Above code gives the auth_url like (altered for security reason)

https://app.box.com/api/oauth2/authorize?state=box_csrf_token_&response_type=code&client_id=otto89rupi6a2snxhzweelt4yvlsa1hu&redirect_uri=https%3A%2F%2Fapp.box.com

and a box_csrf_token_

 

code = 'using_client_Id'
state = 'using_state_from_csrf'

# If csrf token matches, fetch tokens
assert state == 'using_state_from_csrf'
access_token, refresh_token = oauth.authenticate('using_cllient_Id')