Skip to main content

I was able to generate the access token and refresh token but i have to manually click on the auth_url to grant the access to box API to get the auth code. Below is my Python code.

 

 

from boxsdk import Client
from boxsdk import OAuth2
from boxsdk.exception import BoxAPIException
import requests
import logging

oauth = OAuth2(
client_id='client_id',
client_secret='client_secret'
)
csrf_token = ''


global csrf_token
auth_url, csrf_token = oauth.get_authorization_url('http://127.0.0.1:5000/return')

print(csrf_token)
print(auth_url)

 

 

Above code generates auth_url which i have to click to grant the access to API. auth_url takes me to the redirect link which has code at the end

 

 

#get the state from the csrf_token
getState = csrf_token_-16:]
getState

 

 

The redirect link has the code at the end which i used in below code. 

 

state = getState
access_token, refresh_token = oauth.authenticate('code which i get after granting access')
print(access_token)
print(refresh_token)

 

 

 

How can i automate the manual process which i am doing to get the tokens.

 

FYI: The code is for my own use to sort the files in my box account based on file type and size. It does not interact with any external user.


Hi ,


 


You'll want to use the JWT / OAuth2 process instead of just OAuth2 to accomplish this. The JWT piece will replace the need for the user to log in to their Box account and accept the permissions of the application. Instead it's all done seamlessly behind the scenes. 


 


Here's more on the JWT app setup process: https://developer.box.com/docs/setting-up-a-jwt-app


 


Here's a walkthrough of getting the access token, including Python samples: https://developer.box.com/docs/authenticate-with-jwt


 


Hope that helps,


Jon



Hi, 

 

using the resources provided, now i am getting following error. Can you please let me know what i am doing wrong and how to fix it. 

 

Error - 

ValueError: Could not deserialize key data.

Code

from boxsdk import JWTAuth
from boxsdk import Client

# Configure JWT auth object
sdk = JWTAuth(
client_id="my_client_id",
client_secret="my_client_Secret",
enterprise_id="enterprise_Id",
jwt_key_id="key_id",
rsa_private_key_file_sys_path = "C:/Users/Downloads/boxAPIAccess.pem",
rsa_private_key_passphrase = b'pass_phrase'
)

 



Reply