Author Posts

November 12, 2017 at 5:19 pm

I have a script where I am trying to import the box windows sdk (a bunch of dot net assemblies) to use for managing a box environment (for those who want to replicate, download nuget command line tool and run "nuget install box.v2"). However, I have run into a snag. I have gotten as far as constructing the Json Web token and configuring the client to make api calls, but it seems that all of the methods which are used to perform administrative tasks are asynchronous functions. Below is the code I run:

[Reflection.Assembly]::LoadFile("C:\Users\whiggs\Desktop\nuget\System.IdentityModel.Tokens.Jwt.5.1.4\lib\net45\System.IdentityModel.Tokens.Jwt.dll")
[Reflection.Assembly]::LoadFile("C:\Users\whiggs\Desktop\nuget\BouncyCastle.1.8.1\lib\BouncyCastle.Crypto.dll")
[Reflection.Assembly]::LoadFile("C:\Users\whiggs\Desktop\nuget\Box.V2.3.2.0\lib\net45\Box.V2.dll")
[Reflection.Assembly]::LoadFile("C:\Users\whiggs\Desktop\nuget\Microsoft.IdentityModel.Logging.1.1.4\lib\net45\Microsoft.IdentityModel.Logging.dll")
[Reflection.Assembly]::LoadFile("C:\Users\whiggs\Desktop\nuget\Microsoft.IdentityModel.Tokens.5.1.4\lib\net45\Microsoft.IdentityModel.Tokens.dll")
[Reflection.Assembly]::LoadFile("C:\Users\whiggs\Desktop\nuget\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll")
#[Reflection.Assembly]::LoadFile("C:\Users\whiggs\Documents\GitHub\boxcli\BoxCLI\bin\Debug\netcoreapp2.0\box.dll")
#$testob = New-Object Box.V2.config.BoxConfig.Createfromjsonfile (Get-content "C:\Users\whiggs\OneDrive – Acuity Brands, Inc\tryagain.json")
$content = Get-content "C:\Users\whiggs\OneDrive – Acuity Brands, Inc\tryagain.json" | ConvertFrom-Json
$ob = New-Object Box.V2.config.BoxConfig (($content.boxAppSettings).clientID, ($content.boxAppSettings).clientSecret, $content.enterpriseID, (($content.boxAppSettings).appAuth).privateKey, (($content.boxAppSettings).appAuth).passphrase, (($content.boxAppSettings).appAuth).publicKeyID)
$ob2 = New-Object Box.V2.JWTAuth.BoxJWTAuth ($ob)
$admintok = $ob2.AdminToken
$serviceaccount = $ob2.AdminClient($admintok)
$users = $serviceaccount.UsersManager.GetEnterpriseUsersAsync()
$users

Which results in the below exception:

GAC Version Location
— ——- ——–
False v4.0.30319 C:\Users\whiggs\Desktop\nuget\System.IdentityModel.Tokens.Jwt.5.1.4\lib\net45\System...
True v1.1.4322 C:\WINDOWS\assembly\GAC\BouncyCastle.Crypto\1.8.1.0__0e99375e54769942\BouncyCastle.C...
False v4.0.30319 C:\Users\whiggs\Desktop\nuget\Box.V2.3.2.0\lib\net45\Box.V2.dll
False v4.0.30319 C:\Users\whiggs\Desktop\nuget\Microsoft.IdentityModel.Logging.1.1.4\lib\net45\Micros...
False v4.0.30319 C:\Users\whiggs\Desktop\nuget\Microsoft.IdentityModel.Tokens.5.1.4\lib\net45\Microso...
False v4.0.30319 C:\Users\whiggs\Desktop\nuget\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll

Result :
Id : 130652
Exception : System.AggregateException: One or more errors occurred. —>
System.TypeInitializationException: The type initializer for
'System.IdentityModel.Tokens.Jwt.JsonExtensions' threw an exception. —>
System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json,
Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its
dependencies. The system cannot find the file specified.
at System.IdentityModel.Tokens.Jwt.JsonExtensions..cctor()
— End of inner exception stack trace —
at System.IdentityModel.Tokens.Jwt.JsonExtensions.SerializeToJson(Object value)
at System.IdentityModel.Tokens.Jwt.JwtPayload.Base64UrlEncode()
at
System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.WriteToken(SecurityToken
token)
at Box.V2.JWTAuth.BoxJWTAuth.ConstructJWTAssertion(String sub, String boxSubType)
at Box.V2.JWTAuth.BoxJWTAuth.AdminToken()
at Box.V2.JWTAuth.JWTAuthRepository.d__21.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotific
ation(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.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotific
ation(Task task)
at Box.V2.Managers.BoxResourceManager.d__10`1.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotific
ation(Task task)
at Box.V2.Managers.BoxResourceManager.d__9`1.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotific
ation(Task task)
at Box.V2.Managers.BoxUsersManager.d__4.MoveNext()
— End of inner exception stack trace —
—> (Inner Exception #0) System.TypeInitializationException: The type initializer
for 'System.IdentityModel.Tokens.Jwt.JsonExtensions' threw an exception. —>
System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json,
Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its
dependencies. The system cannot find the file specified.
at System.IdentityModel.Tokens.Jwt.JsonExtensions..cctor()
— End of inner exception stack trace —
at System.IdentityModel.Tokens.Jwt.JsonExtensions.SerializeToJson(Object value)
at System.IdentityModel.Tokens.Jwt.JwtPayload.Base64UrlEncode()
at
System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.WriteToken(SecurityToken
token)
at Box.V2.JWTAuth.BoxJWTAuth.ConstructJWTAssertion(String sub, String boxSubType)
at Box.V2.JWTAuth.BoxJWTAuth.AdminToken()
at Box.V2.JWTAuth.JWTAuthRepository.d__21.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotific
ation(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.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotific
ation(Task task)
at Box.V2.Managers.BoxResourceManager.d__10`1.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotific
ation(Task task)
at Box.V2.Managers.BoxResourceManager.d__9`1.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotific
ation(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

I would really appreciate some help. I don't know if the code is failing because the function is asynchronous and I need to do something special to get it to complete successfully, but I am following the example code posted in the github repository for the box windows sdk here but can't seem to successfully execute any of the methods. Please help

November 15, 2017 at 1:28 am

Slow down...not everyone all at once now..

November 16, 2017 at 12:35 pm

No need to get snarky! 😉 😀 ... You might get better / faster / more results on a developer forum like stackoverflow.

November 16, 2017 at 9:23 pm

It is not failing because it's asynchronous, it has non-direct dependency on Newtonsoft.json which you can see by reading the exception:

System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json,
Version=9.0.0.0
, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its
dependencies. The system cannot find the file specified.

So to solve this, just get the library from nuget and import it and then you will see.

I would recommend calling GetAwaiter().GetResult() on the Async() method, or .Result otherwise you are not awaiting the call and it will likely complete without producing any result. (depends which one will deadlock on you, and if you love aggregate exceptions)