Level up your Box knowledge with brand new learning paths on Box University. Visit training.box.com to get started

JWT: not able to get access token

New post

Comments

15 comments

  • gjregions

    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?

    0
    Comment actions Permalink
  • gjregions

    I wasn't expecting this but when I tried this it appears I have an issue only when using .NET framework. If I attempt to obtain a token using .NET Core it works without issue. I tested using the nuget packages Box.V2 for framework and Box.V2.Core for core.

    0
    Comment actions Permalink
  • gjregions

    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. 

    0
    Comment actions Permalink
  • gjregions

    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.

    0
    Comment actions Permalink
  • ededington

     Thank you for your work on this. I am trying to do this from a .NET MVC web application. There is no "Main" program. I am not sure how I can request an access token from a web application

    0
    Comment actions Permalink
  • dfkasidojdsaskf

    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.

     

    token.jpg

     

    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:

     

    https://community.box.com/t5/Platform-and-Development-Forum/Box-V2-JWTAuth-BoxJWTAuth-GetToken-throws-NullReferenceException/m-p/83363#M8138

     

    https://community.box.com/t5/Platform-and-Development-Forum/Getting-an-Error-when-creating-a-User-Access-Tokens-without-SDKs/m-p/83283#M8094

     

    https://community.box.com/t5/Platform-and-Development-Forum/Can-t-get-access-token-when-Salesforce-self-signed-certificate/m-p/83339#M8125

     

    0
    Comment actions Permalink
  • Dimos

    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.

    0
    Comment actions Permalink
  • alfredbr

    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".

    alfredbr_1-1588954721191.png

    The response status code from the POST to https://api.box.com/oauth2/token from the Console app is  “200 - OK” 

     

    alfredbr_0-1588955101209.png

     

     

    I hope this adds to the conversation.  I really want this to work.

     
     

     

     

    0
    Comment actions Permalink
  • alfredbr

    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!

     

    0
    Comment actions Permalink
  • alfredbr

    Alternatively, if you have the Box.V2 SDK source, you can add .ConfigureAwait(false); to the end of line #41 in HttpRequestHandler.cs

     

    alfredbr_0-1588966880294.png

     

    0
    Comment actions Permalink
  • towns

    Hey Everyone, 

     

    We are aware of the issue with getting Admin Tokens using the .NET SDK. We have a PR that is working its way through the process. Once that PR lands successfully we will cut a new release of the SDK. Our ETA for this is early next week. 


    Thanks,

    John Townsend

    0
    Comment actions Permalink
  • alfredbr

    We also need a .ConfigureAwait(false); at the end of line 71 in HttpRequestHandler.cs in the ExecuteAsync() method.  (This is in addition to the one on line 41)

     

     

    0
    Comment actions Permalink
  • KazumasaBaba

    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.

    0
    Comment actions Permalink
  • bradtx19

    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

     

    0
    Comment actions Permalink
  • Stroltz

    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) 

     

     

     

     

     

    0
    Comment actions Permalink

Please sign in to leave a comment.