Skip to main content
Question

Cannot GET access tokens using Authorization code in .net console app, which work in POST MAN

  • May 22, 2025
  • 3 replies
  • 40 views

Forum|alt.badge.img

Hi All, I like to seek help on a issue i am facing when i try to authenticate into BOX.

 

Step 1: i used https://account.box.com/api/oauth2/authorize?response_type=code&client_id="my id"

             to get Auth code.

Step 2: i tried to get Access & refresh tokens using postman it worked fine.

 

Step 3: Now i tried to automate process of getting Access and refresh tokens using .net code.

 

string param = string.Format("grant_type=authorization_code&code={0}&client_id={1}&client_secret={2}", "mycode", "myid", "mysecret");

var client = new RestClient("https://pfizer.app.box.com/oauth2/token/");

var request = new RestRequest(Method.POST);

request.AddHeader("content-type", "application/x-www-form-urlencoded");

request.AddParameter("application/x-www-form-urlencoded", param, ParameterType.RequestBody);

var response = client.Execute(request);

var json = JObject.Parse(response.Content);

Console.WriteLine(json);

 

I always get Bad request ,

{ "error": "invalid_grant", "error_description": "The authorization code has expired" }

 

I am not using auth code 2 times anywhere, even fresh token does not give any response.

Reference article:

https://stackoverflow.com/questions/40826251/get-an-access-token-from-box-api-from-asp-net-application?rq=1

Please help.

Thanks.

3 replies

Forum|alt.badge.img

Hey , 

 

Curious if there is a gap between when you retrieve the auth code and your first time trying to generate an access token? The reason being the auth code expires after 60 seconds so you will need to make the call to generate an access token before then. 

 

 


Forum|alt.badge.img

Yeah I made sure I did it before 20 secs.


Forum|alt.badge.img

 is it possible to increate the expiration time? 60s is very short!