Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PowerBuilder) Box.com OAuth2 with JSON Web TokensSee more Box ExamplesDemonstrates how to obtain an OAuth2 access token using a JSON Web Token. The following explanation is copied from Box Authentication Models
integer li_rc oleobject loo_JsonRsaKey integer li_Success string ls_Passphrase string ls_PrivateKeyPem oleobject loo_RsaKey oleobject loo_Jwt oleobject loo_Jose oleobject loo_Claims oleobject loo_Prng oleobject loo_Rest integer li_BAutoReconnect string ls_JsonResponse oleobject loo_JResponse string ls_AccessToken // This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // When you created an RSA key pair using the Box web user interface, // you downloaded a json file named something like "7152782_kkdxptq2_config.json" // This contains the following: // { // "boxAppSettings": { // "clientID": "0kraci84o0jfr7yuw596tf394iigzbe7", // "clientSecret": "xxxxxxxxxxxxxxxxxxxxxxxxx", // "appAuth": { // "publicKeyID": "kkdxptq2", // "privateKey": "-----BEGIN ENCRYPTED PRIVATE KEY-----\nMIIFDj ... nceU=\n-----END ENCRYPTED PRIVATE KEY-----\n", // "passphrase": "xxxxxxxxxxxxxxxxxxxxxxxx" // } // }, // "enterpriseID": "7152782" // } // // Load it into a Chilkat JSON object to allow access to the content. loo_JsonRsaKey = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_JsonRsaKey.ConnectToNewObject("Chilkat.JsonObject") if li_rc < 0 then destroy loo_JsonRsaKey MessageBox("Error","Connecting to COM object failed") return end if li_Success = loo_JsonRsaKey.LoadFile("qa_data/tokens/7152782_kkdxptq2_config.json") // Load the private key into a Chilkat private key object. ls_Passphrase = loo_JsonRsaKey.StringOf("boxAppSettings.appAuth.passphrase") ls_PrivateKeyPem = loo_JsonRsaKey.StringOf("boxAppSettings.appAuth.privateKey") loo_RsaKey = create oleobject // Use "Chilkat_9_5_0.PrivateKey" for versions of Chilkat < 10.0.0 li_rc = loo_RsaKey.ConnectToNewObject("Chilkat.PrivateKey") li_Success = loo_RsaKey.LoadEncryptedPem(ls_PrivateKeyPem,ls_Passphrase) if li_Success <> 1 then Write-Debug loo_RsaKey.LastErrorText destroy loo_JsonRsaKey destroy loo_RsaKey return end if // The JSON Web Token will be created using the JWT class loo_Jwt = create oleobject // Use "Chilkat_9_5_0.Jwt" for versions of Chilkat < 10.0.0 li_rc = loo_Jwt.ConnectToNewObject("Chilkat.Jwt") // Construct the JOSE header... loo_Jose = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_Jose.ConnectToNewObject("Chilkat.JsonObject") // Chilkat supports the following algorithms: "RS256", "RS384", and "RS512". (Chilkat also supports other algorithms that Box does not yet support.) loo_Jose.UpdateString("alg","RS256") loo_Jose.UpdateString("typ","JWT") loo_Jose.UpdateString("kid",loo_JsonRsaKey.StringOf("boxAppSettings.appAuth.publicKeyID")) // Now let's build the JWT claims. Most of this is just boilerplate (i.e. the same every time..) // The JWT claims contain these required and optional elements: // iss (required, String) The Client ID of the service that created the JWT assertion. // sub (required, String) enterprise_id for a token specific to an enterprise when creating and managing app users, or the app user_id for a token specific to an individual app user. // box_sub_type (required, String) "enterprise" or "user" depending on the type of token being requested in the sub claim. // aud (required, String) Always "https://api.box.com/oauth2/token" for OAuth2 token requests // jti (required, String) A universally unique identifier specified by the client for this JWT. This is a unique string that is at least 16 characters and at most 128 characters. // exp (required, NumericDate) The unix time as to when this JWT will expire. This can be set to a maximum value of 60 seconds beyond the issue time. Note: It is recommended to set this value to less than the maximum allowed 60 seconds. // iat (optional, NumericDate) Issued at time. The token cannot be used before this time. // nbf (optional, NumericDate) Not before. Specifies when the token will start being valid. // loo_Claims = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_Claims.ConnectToNewObject("Chilkat.JsonObject") loo_Claims.UpdateString("iss",loo_JsonRsaKey.StringOf("boxAppSettings.clientID")) loo_Claims.UpdateString("sub",loo_JsonRsaKey.StringOf("enterpriseID")) loo_Claims.UpdateString("box_sub_type","enterprise") loo_Claims.UpdateString("aud","https://api.box.com/oauth2/token") // Generate 32 random bytes (base64 encoded) for the "jti" loo_Prng = create oleobject // Use "Chilkat_9_5_0.Prng" for versions of Chilkat < 10.0.0 li_rc = loo_Prng.ConnectToNewObject("Chilkat.Prng") loo_Claims.UpdateString("jti",loo_Prng.GenRandom(32,"base64")) // Set the expiration time to 60 seconds after the current time. loo_Claims.UpdateInt("exp",loo_Jwt.GenNumericDate(60)) // We're going to do the following POST to get a JSON response that contains our OAuth2 access token: // POST /oauth2/token // Content-Type: application/x-www-form-urlencoded // grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer& // assertion=<JWT>& // client_id=<client_id>& // client_secret=<client_secret> // First, make the initial connection. // A single REST object, once connected, can be used for many Box REST API calls. // The auto-reconnect indicates that if the already-established HTTPS connection is closed, // then it will be automatically re-established as needed. loo_Rest = create oleobject // Use "Chilkat_9_5_0.Rest" for versions of Chilkat < 10.0.0 li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest") li_BAutoReconnect = 1 li_Success = loo_Rest.Connect("api.box.com",443,1,li_BAutoReconnect) if li_Success <> 1 then Write-Debug loo_Rest.LastErrorText destroy loo_JsonRsaKey destroy loo_RsaKey destroy loo_Jwt destroy loo_Jose destroy loo_Claims destroy loo_Prng destroy loo_Rest return end if // Add the query params. // Calling ClearAllParts is wise if previous requests were sent prior to this one on the same REST object.. loo_Rest.ClearAllParts() loo_Rest.AddQueryParam("grant_type","urn:ietf:params:oauth:grant-type:jwt-bearer") loo_Rest.AddQueryParam("client_id",loo_JsonRsaKey.StringOf("boxAppSettings.clientID")) loo_Rest.AddQueryParam("client_secret",loo_JsonRsaKey.StringOf("boxAppSettings.clientSecret")) loo_Rest.AddQueryParam("assertion",loo_Jwt.CreateJwtPk(loo_Jose.Emit(),loo_Claims.Emit(),loo_RsaKey)) ls_JsonResponse = loo_Rest.FullRequestFormUrlEncoded("POST","/oauth2/token") if loo_Rest.LastMethodSuccess <> 1 then Write-Debug loo_Rest.LastErrorText destroy loo_JsonRsaKey destroy loo_RsaKey destroy loo_Jwt destroy loo_Jose destroy loo_Claims destroy loo_Prng destroy loo_Rest return end if // If successful, we'll get a response status code equal to 200, // and a JSON response that looks like this: // { // "access_token": "mNr1FrCvOeWiGnwLL0OcTL0Lux5jbyBa", // "expires_in": 4169, // "restricted_to": [], // "token_type": "bearer" // } // loo_JResponse = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_JResponse.ConnectToNewObject("Chilkat.JsonObject") loo_JResponse.EmitCompact = 0 loo_JResponse.Load(ls_JsonResponse) if loo_Rest.ResponseStatusCode <> 200 then Write-Debug loo_JResponse.Emit() Write-Debug "Failed." destroy loo_JsonRsaKey destroy loo_RsaKey destroy loo_Jwt destroy loo_Jose destroy loo_Claims destroy loo_Prng destroy loo_Rest destroy loo_JResponse return end if Write-Debug loo_JResponse.Emit() // Get the access token: ls_AccessToken = loo_JResponse.StringOf("access_token") Write-Debug "Access token, valid for 60 minutes: " + ls_AccessToken destroy loo_JsonRsaKey destroy loo_RsaKey destroy loo_Jwt destroy loo_Jose destroy loo_Claims destroy loo_Prng destroy loo_Rest destroy loo_JResponse |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.