PureBasic Requires Chilkat v11.5.0+
PureBasic
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 PureBasic Downloads
IncludeFile "CkOAuth2.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 0
; Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
; See Global Unlock Sample.
; Guide: Box API Guide.
success = 0
oauth2.i = CkOAuth2::ckCreate()
If oauth2.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkOAuth2::setCkListenPort(oauth2, 3017)
CkOAuth2::setCkAuthorizationEndpoint(oauth2, "https://account.box.com/api/oauth2/authorize")
CkOAuth2::setCkTokenEndpoint(oauth2, "https://api.box.com/oauth2/token")
CkOAuth2::setCkClientId(oauth2, "BOX_CLIENT_ID")
CkOAuth2::setCkClientSecret(oauth2, "BOX_CLIENT_SECRET")
; Use Box's documented confidential-client authorization-code flow.
CkOAuth2::setCkCodeChallenge(oauth2, 0)
url.s = CkOAuth2::ckStartAuth(oauth2)
If CkOAuth2::ckLastMethodSuccess(oauth2) <> 1
Debug CkOAuth2::ckLastErrorText(oauth2)
CkOAuth2::ckDispose(oauth2)
ProcedureReturn
EndIf
success = CkOAuth2::ckLaunchBrowser(oauth2,url)
If success <> 1
Debug CkOAuth2::ckLastErrorText(oauth2)
CkOAuth2::ckDispose(oauth2)
ProcedureReturn
EndIf
elapsedMs.i = 0
While (elapsedMs < 120000) AND (CkOAuth2::ckAuthFlowState(oauth2) < 3)
CkOAuth2::ckSleepMs(oauth2,100)
elapsedMs = elapsedMs + 100
Wend
If CkOAuth2::ckAuthFlowState(oauth2) < 3
CkOAuth2::ckCancel(oauth2)
Debug "Authorization timed out."
CkOAuth2::ckDispose(oauth2)
ProcedureReturn
EndIf
If CkOAuth2::ckAuthFlowState(oauth2) <> 3
Debug "Authorization was denied or failed. State: " + Str(CkOAuth2::ckAuthFlowState(oauth2))
CkOAuth2::ckDispose(oauth2)
ProcedureReturn
EndIf
; Retain both tokens in your application's protected token store.
; The variables below are deliberately not printed.
accessToken.s = CkOAuth2::ckAccessToken(oauth2)
refreshToken.s = CkOAuth2::ckRefreshToken(oauth2)
; 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.
tokens.i = CkJsonObject::ckCreate()
If tokens.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoad(tokens,CkOAuth2::ckAccessTokenResponse(oauth2))
If success <> 1
Debug CkJsonObject::ckLastErrorText(tokens)
CkOAuth2::ckDispose(oauth2)
CkJsonObject::ckDispose(tokens)
ProcedureReturn
EndIf
success = CkJsonObject::ckWriteFile(tokens,"qa_data/tokens/box_oauth.json")
If success <> 1
Debug CkJsonObject::ckLastErrorText(tokens)
CkOAuth2::ckDispose(oauth2)
CkJsonObject::ckDispose(tokens)
ProcedureReturn
EndIf
Debug "Authorization complete; token response saved in the protected local token file."
CkOAuth2::ckDispose(oauth2)
CkJsonObject::ckDispose(tokens)
ProcedureReturn
EndProcedure