For those of you who are looking for information on how to integrate server-side box authentication using OAuth 2.0 and JWT in powershell and have not been able to find any information or get any legitimate help from Box support, then this post is for you. I spent a lot of time trying to get any kind of help when it came to this, so now that I was able to find the answer myself (no thanks to box support of course), I figured I would post the solution here to help those who find themselves in the position I was in might and give them the info they need.
My solution utilizes the box windows sdk v2, so head on over to nuget's website, download the latest command line nuget executable, and run the below command in a command prompt:
nuget install box.v2
This will download all of the dll files you will need to continue perform the task. Place the folders/files somewhere where you can store them long term, then create a new powershell script with the below content (the below code assumes that you generated your public/private key pair using the box development console and have the downloaded Json file with all the important app information):
[Reflection.Assembly]::LoadFile("Full\Path\to\System.IdentityModel.Tokens.Jwt.5.1.4\lib\net45\System.IdentityModel.Tokens.Jwt.dll") [Reflection.Assembly]::LoadFile("Full\Path\to\BouncyCastle.1.8.1\lib\BouncyCastle.Crypto.dll") [Reflection.Assembly]::LoadFile("Full\Path\to\Box.V184.108.40.206\lib\net45\Box.V2.dll") [Reflection.Assembly]::LoadFile("Full\Path\to\Microsoft.IdentityModel.Logging.1.1.4\lib\net45\Microsoft.IdentityModel.Logging.dll") [Reflection.Assembly]::LoadFile("Full\Path\to\Microsoft.IdentityModel.Tokens.5.1.4\lib\net45\Microsoft.IdentityModel.Tokens.dll") [Reflection.Assembly]::LoadFile("Full\Path\to\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll") $content = Get-content "Full\Path\to\myjsonfile.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)
And now the variable "service account" is an object which contains methods you can use to perform administrative functions in your box environment (rather than the api calls that most are familiar with). Have a blast.
Please sign in to leave a comment.