Skip to main content

I am trying to upload file box folder using python but i am unable to get refresh token

  • April 18, 2024
  • 1 reply
  • 183 views

Here is the code

from boxsdk import OAuth2, Client

CLIENT_ID = 'XXXXXXXXXXXXXXXXXXX'
CLIENT_SECRET = 'XXXXXXXXXXXXXXXXXX'

oauth2 = OAuth2(
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET,
)

access_token = oauth2.access_token

client = Client(oauth2)

folder_id = '1234'
folder = client.folder(folder_id=folder_id).get()


file_path = "C:\\Users\\sample.txt"
file_name = 'sample.txt'

with open(file_path, 'rb') as f:
    file_uploaded = folder.upload_stream(f, file_name)

print(f'File {file_uploaded.name} uploaded successfully to {folder.name} folder.')
Error
boxsdk.exception.BoxOAuthException:
Message: No "refresh_token" parameter found
Status: 400
URL: https://api.box.com/oauth2/token
Method: POST

1 reply

rbarbosa Box
  • Developer Advocate
  • 553 replies
  • April 19, 2024

Hi @user222 (Sami), welcome to the forum!!!

To get a OAuth 2.0 access token, the user must first authorize the app.

In this process you’ll get both an access token, valid for 60 minutes, and a refresh token valid for 60 days.

Subsequent requests will use the refresh token to get a new access and refresh tokens.

Looks like you’re missing the first steps. These include redirecting the user to an authorization page, receiving a code from box and using that code to get the first token pair.

From your sample you’re using the classing python SDK, and I wan to mention there is a new Next Gen Python SDK available.

We have python workshops for both SDKs, which include sample code for several authentication modes.

For classic SDK workshop, look for the test_oauth.py and the box_client.py. (under utils)

For the Next Gen SDK workshop, look for test_oauth.py, and the box_client_oauth.py (under utils)

These should get you started with OAuth 2.0.

We do support more application friendly authentication modes, such as Client Credential Grants (CCG) and JSON Web Token (JWT)

Examples for all of those are included in the workshops, and we also have guides on this topic.

Let us know if this helps, and if you want to take a deeper dive into any topic or use case.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings