JWT: not able to get access token
I am using Box .NET SDK. I have created a Box App with JWT and it has been authorized by admin. If I use a developer token all is well. If I try to get a UserToken or AdminToken the application hangs on the call to get the token. When I step through my code the call to AdminToken does not return, it just hangs.
var reader = new StreamReader(HostingEnvironment.MapPath("~/Scripts/BoxConfig.json"));
var json = reader.ReadToEnd();
var config = BoxConfig.CreateFromJsonString(json);
BoxJWTAuth boxJWT = new BoxJWTAuth(config);
var userToken = boxJWT.AdminToken();
-
I am having this same issue. The developer token works w/out any issues but the I hang on the Session.AdminToken(); call everytime. I'm not sure what I am missing as I have tried updating every option in the custom app configuration I can think of, and even reauthorizing each time. Is there something wrong with the latest SDK for .NET framework?
-
Here is some more added details I've found while digging into this issue:
The JWT AdminToken can be created with a .NET Core 3.0 Windows Console build.
I cannot get a token created using .NET Framework 4.5, 4.6, 4.7 OR .NET Core 3.0, 3.1 Windows Forms build.
There is some reason that the Windows Forms will not work but the Console works just fine.
-
Ok, I think I may have found a solution/workaround.
In my program I'm trying to kickoff the authentication on the form constructor class and it always hangs. However, if I initialize the session and attempt to get a token in the 'Main" program I'm able to get one successfully. Not sure why the token can't pull from within a class but it works in the 'Main' so maybe that will help you.
-
I'm also experiencing this same issue. I have two different Core 3.1 console apps, running on different servers, and both are throwing a NullReferenceException when attempting to get the AdminToken. The line in red is the one throwing the exception.
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at Box.V2.JWTAuth.BoxJWTAuth.GetToken(String subType, String subId)
at Box.V2.JWTAuth.BoxJWTAuth.AdminToken()This error has been surfacing on and off over the last couple of days. At certain times, both apps fail with this exception. Later on, both will be working normally again and then later on both will be failing again.
Some other recent threads involving difficulty with tokens. The first is similar to what I'm experiencing, the other two involve a 400 error that may or may not be related:
-
Same here. I have a win forms poc i did a couple of months ago, it was working just fine, now it just hangs. I updated the packages, nothing. I then started looking into it, tried a copy paste of the .net core example, new application, and now i am getting (on .net core) the null reference exception.
-
I'm having the same problem, and I have not found a solution, but I want to add to the conversation.
In particular, I want to eliminate any concerns about various versions of .NET involved.
I downloaded the Box.V2 SDK source code from GitHub and linked it into my code. (If you want to do this yourself, (a) download the box-windows-sdk-v2-master.zip file from GitHub, (b) remove the NuGet reference to Box.V2 from your project, (c) add the Box.V2.csproj to your solution, (d) reference the (now local) Box.V2 project and (e) recompile.)
What I have found is the hang occurs in HttpRequestHandler.cs on line #177 in Box.V2 when linked to my ASP.NET application, but not when linked to a Console application. There are no other differences in what I am doing to get a Box token, i.e. I am using the same computer, same user, same Box config file, same version of .NET, same VisualStudio, etc.
Below is a QuickWatch of the response object on line #177 in HttpRequestHandler.cs. This is running in my ASP.NET MVC application. The response status code from the POST to https://api.box.com/oauth2/token is “400 - Bad Request".
The response status code from the POST to https://api.box.com/oauth2/token from the Console app is “200 - OK”
I hope this adds to the conversation. I really want this to work.
-
A hint from on this thread (https://community.box.com/t5/Platform-and-Development-Forum/NET-SDK-code-hangs-at-AdminToken/m-p/56258#M4626) led me to this approach:
Yes, you can get this to work if you create the AdminToken in a thread other than your main controller thread.
This is what worked for me:
BoxClient _boxClient = null; var thread = new System.Threading.Thread(() => { var boxJWT = new BoxJWTAuth(boxConfig); // create boxConfig elsewhere var adminToken = boxJWT.AdminToken(); // create adminToken in new thread _boxClient = boxJWT.AdminClient(adminToken); // set your _boxClient }); thread.Start(); // start the thread thread.Join(); // wait for the thread to finish (ugly code). Debug.Assert(_boxClient != null); // success!
-
I have same issue, but I had solved fortunately.
Did everyone set client-id at the page(https://app.box.com/master/custom-apps)?
Once you set your application's client-id and push "Authorize New App" button at the page, this issue may be solved.
If you don't set a client-id as an Authorized App, "Box.V2.Core 3.2x" will return "NullReferenceException" and "Box.V2.Core 3.10.0" will return "BadRequest".I think this root cause is showing an inappropriate error message by "Box.V2.Core 3.2x".
Because in case of using "Box.V2.Core 3.10.0", we can recognize what we should do to solve this issue. -
Any updates on this problem?
My app works fine on the desktop VS Studio environment I developed it on as well as a Windows Server box. I have copied my code to my laptop and I can't get past the call to AdminToken.
var boxConfig = new BoxConfig(CLIENT_ID, CLIENT_SECRET, ENTERPRISE_ID, privateKey, JWT_PRIVATE_KEY_PASSWORD, JWT_PUBLIC_KEY_ID);
var boxJWT = new BoxJWTAuth(boxConfig);var adminToken = boxJWT.AdminToken();
I am getting this exception::
System.NullReferenceException
-
Hi,
I am getting a similar problem. (.NET SDK) Running box 3.24.0
System.NullReferenceException: Object reference not set to an instance of an object.
at Box.V2.JWTAuth.BoxJWTAuth.GetToken(String subType, String subId)at Processing.UploadToBoxProcessingStep.d__46.MoveNext()
Is very strange. I am consistently getting it on one desktop. I have tried 3 other machines on the same network, all fully patched Windows 10 and they work just fine. I can't explain what my be different about that PC to throw the token error. (System File Checker can't see anything)
Please sign in to leave a comment.
Comments
15 comments