DataFlex Requires Chilkat v11.5.0+
DataFlex
Box: Authorize a User with OAuth 2.0
See more Box Examples
Interactive local-development example using a browser and a loopback callback. Register exactly http://localhost:3017/ in the Box User app. Run this in a trusted environment that can hold the client secret. A deployed web app must handle its registered HTTPS callback on the server.
See the Box API guide for setup and the complete workflow.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoOauth2
String sUrl
Integer iElapsedMs
String sAccessToken
String sRefreshToken
Handle hoTokens
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
// See Global Unlock Sample.
// Guide: Box API Guide.
Move False To iSuccess
Get Create (RefClass(cComChilkatOAuth2)) To hoOauth2
If (Not(IsComObjectCreated(hoOauth2))) Begin
Send CreateComObject of hoOauth2
End
Set ComListenPort Of hoOauth2 To 3017
Set ComAuthorizationEndpoint Of hoOauth2 To "https://account.box.com/api/oauth2/authorize"
Set ComTokenEndpoint Of hoOauth2 To "https://api.box.com/oauth2/token"
Set ComClientId Of hoOauth2 To "BOX_CLIENT_ID"
Set ComClientSecret Of hoOauth2 To "BOX_CLIENT_SECRET"
// Use Box's documented confidential-client authorization-code flow.
Set ComCodeChallenge Of hoOauth2 To False
Get ComStartAuth Of hoOauth2 To sUrl
Get ComLastMethodSuccess Of hoOauth2 To bTemp1
If (bTemp1 <> True) Begin
Get ComLastErrorText Of hoOauth2 To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComLaunchBrowser Of hoOauth2 sUrl To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoOauth2 To sTemp1
Showln sTemp1
Procedure_Return
End
Move 0 To iElapsedMs
While ((iElapsedMs < 120000) And ((ComAuthFlowState(hoOauth2)) < 3))
Send ComSleepMs To hoOauth2 100
Move (iElapsedMs + 100) To iElapsedMs
Loop
Get ComAuthFlowState Of hoOauth2 To iTemp1
If (iTemp1 < 3) Begin
Get ComCancel Of hoOauth2 To iSuccess
Showln "Authorization timed out."
Procedure_Return
End
Get ComAuthFlowState Of hoOauth2 To iTemp1
If (iTemp1 <> 3) Begin
Get ComAuthFlowState Of hoOauth2 To iTemp1
Showln "Authorization was denied or failed. State: " iTemp1
Procedure_Return
End
// Retain both tokens in your application's protected token store.
// The variables below are deliberately not printed.
Get ComAccessToken Of hoOauth2 To sAccessToken
Get ComRefreshToken Of hoOauth2 To sRefreshToken
// For this local-development example only, save in a directory restricted to this user.
// Create qa_data/tokens before running; never put this file under a web root.
Get Create (RefClass(cComChilkatJsonObject)) To hoTokens
If (Not(IsComObjectCreated(hoTokens))) Begin
Send CreateComObject of hoTokens
End
Get ComAccessTokenResponse Of hoOauth2 To sTemp1
Get ComLoad Of hoTokens sTemp1 To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoTokens To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComWriteFile Of hoTokens "qa_data/tokens/box_oauth.json" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoTokens To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "Authorization complete; token response saved in the protected local token file."
End_Procedure