Skip to main content
Question

Java code to get the authorization code and access code

  • May 22, 2025
  • 3 replies
  • 46 views

Forum|alt.badge.img

Hi Team,

 I have gone through the api to get the authorization code  for the access token and found the below code 

res.redirect(box_redirect);

 

Could anyone provide complete code snippet for the same.

 

Thanks & Regards,

Kalyan

3 replies

Forum|alt.badge.img

 This step in the OAuth2 flow represents literally redirecting a user who is accessing your service via a web browser or mobile device to the Box authorization URL where they will log in with their credentials and verify that your application is permitted to access their Box account.  Exactly how this redirect is performed depends on what web framework or mobile device you're running your service on.

 

In the event that you're not running a web or mobile application and instead want to generate tokens programmatically (without an end user providing their credentials), JWT Authentication is a better option.  You can read more about why you might want to choose JWT authand how to get started using JWT auth in the Box Developer documentation. 


Forum|alt.badge.img

 

Thanks for your immediate response  but as part of our requirement in our use case need to follow oauth2 instead of JWT.Could  you have sample code using oauth2 approach instead of JWT and it will be helpful for us to proceed.

 

Thanks & Regards,

Kalyan


Forum|alt.badge.img

 Unfortunately, I can't give you sample code for how to redirect a user; it really depends on the exact framework you're using and/or what platform you're on (web server vs mobile device).  For a web application, you should send the user who made the request to your service a 302 status code with a Location header whose value is the Box authorization URL.  On mobile, you'd likely want to open a webview to that same URL.