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.