PowerBuilder Requires Chilkat v11.5.0+
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Oauth2
string ls_Url
long li_ElapsedMs
string ls_AccessToken
string ls_RefreshToken
oleobject loo_Tokens
li_Success = 0
// Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
// See Global Unlock Sample.
// Guide: Box API Guide.
li_Success = 0
loo_Oauth2 = create oleobject
li_rc = loo_Oauth2.ConnectToNewObject("Chilkat.OAuth2")
if li_rc < 0 then
destroy loo_Oauth2
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Oauth2.ListenPort = 3017
loo_Oauth2.AuthorizationEndpoint = "https://account.box.com/api/oauth2/authorize"
loo_Oauth2.TokenEndpoint = "https://api.box.com/oauth2/token"
loo_Oauth2.ClientId = "BOX_CLIENT_ID"
loo_Oauth2.ClientSecret = "BOX_CLIENT_SECRET"
// Use Box's documented confidential-client authorization-code flow.
loo_Oauth2.CodeChallenge = 0
ls_Url = loo_Oauth2.StartAuth()
if loo_Oauth2.LastMethodSuccess <> 1 then
Write-Debug loo_Oauth2.LastErrorText
destroy loo_Oauth2
return
end if
li_Success = loo_Oauth2.LaunchBrowser(ls_Url)
if li_Success <> 1 then
Write-Debug loo_Oauth2.LastErrorText
destroy loo_Oauth2
return
end if
li_ElapsedMs = 0
do while (li_ElapsedMs < 120000) AND (loo_Oauth2.AuthFlowState < 3)
loo_Oauth2.SleepMs(100)
li_ElapsedMs = li_ElapsedMs + 100
loop
if loo_Oauth2.AuthFlowState < 3 then
loo_Oauth2.Cancel()
Write-Debug "Authorization timed out."
destroy loo_Oauth2
return
end if
if loo_Oauth2.AuthFlowState <> 3 then
Write-Debug "Authorization was denied or failed. State: " + string(loo_Oauth2.AuthFlowState)
destroy loo_Oauth2
return
end if
// Retain both tokens in your application's protected token store.
// The variables below are deliberately not printed.
ls_AccessToken = loo_Oauth2.AccessToken
ls_RefreshToken = loo_Oauth2.RefreshToken
// 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.
loo_Tokens = create oleobject
li_rc = loo_Tokens.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Tokens.Load(loo_Oauth2.AccessTokenResponse)
if li_Success <> 1 then
Write-Debug loo_Tokens.LastErrorText
destroy loo_Oauth2
destroy loo_Tokens
return
end if
li_Success = loo_Tokens.WriteFile("qa_data/tokens/box_oauth.json")
if li_Success <> 1 then
Write-Debug loo_Tokens.LastErrorText
destroy loo_Oauth2
destroy loo_Tokens
return
end if
Write-Debug "Authorization complete; token response saved in the protected local token file."
destroy loo_Oauth2
destroy loo_Tokens