Skip to main content
Question

PowerShell JWT Authentication

  • May 22, 2025
  • 2 replies
  • 16 views

Forum|alt.badge.img

Hi,

I am trying to create a PowerShell task to create a new user, but I can't get past the authentication part.

I have tried the script at:
https://community.box.com/t5/Platform-and-Development-Forum/JWT-Oauth-2-0-using-powershell/m-p/57528#M4766

 

This doesn't work for me sadly.
First i had to add the -Raw parameter to the Get-Content part.

When filling in the paths and trying it out with getting the users, I get the following error message:

Result                 : 
Id                     : 121315
Exception              : System.AggregateException: One or more errors occurred. ---> Box.V2.Exceptions.BoxException: The API returned an error [BadRequest]
                            at Box.V2.Extensions.BoxResponseExtensions.ParseResults[T](IBoxResponse`1 response, IBoxConverter converter)
                            at Box.V2.JWTAuth.BoxJWTAuth.JWTAuthPost(String assertion)
                            at Box.V2.JWTAuth.BoxJWTAuth.GetToken(String subType, String subId)
                            at Box.V2.JWTAuth.JWTAuthRepository.d__21.MoveNext()
                         --- End of stack trace from previous location where exception was thrown ---
                            at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
                            at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                            at Box.V2.Managers.BoxResourceManager.d__13`1.MoveNext()
                         --- End of stack trace from previous location where exception was thrown ---
                            at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
                            at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                            at Box.V2.Managers.BoxResourceManager.d__12`1.MoveNext()
                         --- End of stack trace from previous location where exception was thrown ---
                            at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
                            at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                            at Box.V2.Managers.BoxResourceManager.d__11`1.MoveNext()
                         --- End of stack trace from previous location where exception was thrown ---
                            at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
                            at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                            at Box.V2.Managers.BoxUsersManager.d__4.MoveNext()
                            --- End of inner exception stack trace ---
                         ---> (Inner Exception #0) Box.V2.Exceptions.BoxException: The API returned an error [BadRequest]
                            at Box.V2.Extensions.BoxResponseExtensions.ParseResults[T](IBoxResponse`1 response, IBoxConverter converter)
                            at Box.V2.JWTAuth.BoxJWTAuth.JWTAuthPost(String assertion)
                            at Box.V2.JWTAuth.BoxJWTAuth.GetToken(String subType, String subId)
                            at Box.V2.JWTAuth.JWTAuthRepository.d__21.MoveNext()
                         --- End of stack trace from previous location where exception was thrown ---
                            at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
                            at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                            at Box.V2.Managers.BoxResourceManager.d__13`1.MoveNext()
                         --- End of stack trace from previous location where exception was thrown ---
                            at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
                            at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                            at Box.V2.Managers.BoxResourceManager.d__12`1.MoveNext()
                         --- End of stack trace from previous location where exception was thrown ---
                            at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
                            at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                            at Box.V2.Managers.BoxResourceManager.d__11`1.MoveNext()
                         --- End of stack trace from previous location where exception was thrown ---
                            at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
                            at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                            at Box.V2.Managers.BoxUsersManager.d__4.MoveNext()<---
                         
Status                 : Faulted
IsCanceled             : False
IsCompleted            : True
CreationOptions        : None
AsyncState             : 
IsFaulted              : True
AsyncWaitHandle        : System.Threading.ManualResetEvent
CompletedSynchronously : False

So after some fiddling around, I found out that the parameter $admintok doesn't actually contain the admin token;

 

$admintok = $ob2.AdminToken

is no actual API call, it should be 

$admintok = $ob2.AdminToken()

But whenever i switch that up, I get the following error message:

Exception calling "AdminToken" with "0" argument(s): "The API returned an error [BadRequest]"
At line:10 char:1
+ $admintok = $ob2.AdminToken()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : BoxException

 

Any ideas on what is going wrong here?

2 replies

Forum|alt.badge.img

I am having this same issue. any solutions found?


Forum|alt.badge.img

Hi There.

For JWT you need to Authorise your Application to execute the command and generate the Admin token.

Unless you authorise the API will hit you bad request.

For authorise

1. Login to box

2. Go to your JWT applocation from box dev console

3. You would find a tab called " Authorise" on the web

4. Click and submit

5. The admin team of BOX (internal/your organization Box admins will evaluate and authorise)

5. Once authorised is confirmed then you would get the admin token.

I hope this helps.

Happy coding!.