PowerShell JWT Authentication

Highlighted
Occasional Contributor

PowerShell JWT Authentication

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

 

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.<RefreshAccessTokenAsync>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.<RetryExpiredTokenRequest>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.<ExecuteRequest>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.<ToResponseAsync>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.<GetEnterpriseUsersAsync>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.<RefreshAccessTokenAsync>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.<RetryExpiredTokenRequest>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.<ExecuteRequest>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.<ToResponseAsync>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.<GetEnterpriseUsersAsync>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?

Tags (1)
1 REPLY 1
Highlighted
First-time Contributor

Re: PowerShell JWT Authentication

I am having this same issue. any solutions found?

Tags (2)