Sample code for 30+ languages & platforms
Xojo Plugin Requires Chilkat v11.5.0+

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 Xojo Plugin Downloads

Xojo Plugin
Dim success As Boolean
success = False

//  Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
//  See Global Unlock Sample.
//  Guide: Box API Guide.

success = False

Dim oauth2 As New Chilkat.OAuth2
oauth2.ListenPort = 3017
oauth2.AuthorizationEndpoint = "https://account.box.com/api/oauth2/authorize"
oauth2.TokenEndpoint = "https://api.box.com/oauth2/token"
oauth2.ClientId = "BOX_CLIENT_ID"
oauth2.ClientSecret = "BOX_CLIENT_SECRET"
//  Use Box's documented confidential-client authorization-code flow.
oauth2.CodeChallenge = False
Dim url As String
url = oauth2.StartAuth()
If (oauth2.LastMethodSuccess <> True) Then
    System.DebugLog(oauth2.LastErrorText)
    Return
End If

success = oauth2.LaunchBrowser(url)
If (success <> True) Then
    System.DebugLog(oauth2.LastErrorText)
    Return
End If

Dim elapsedMs As Int32
elapsedMs = 0
While (elapsedMs < 120000) And (oauth2.AuthFlowState < 3)
    oauth2.SleepMs 100
    elapsedMs = elapsedMs + 100
Wend
If (oauth2.AuthFlowState < 3) Then
    success = oauth2.Cancel()
    System.DebugLog("Authorization timed out.")
    Return
End If

If (oauth2.AuthFlowState <> 3) Then
    System.DebugLog("Authorization was denied or failed. State: " + Str(oauth2.AuthFlowState))
    Return
End If

//  Retain both tokens in your application's protected token store.
//  The variables below are deliberately not printed.
Dim accessToken As String
accessToken = oauth2.AccessToken
Dim refreshToken As String
refreshToken = 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.
Dim tokens As New Chilkat.JsonObject
success = tokens.Load(oauth2.AccessTokenResponse)
If (success <> True) Then
    System.DebugLog(tokens.LastErrorText)
    Return
End If

success = tokens.WriteFile("qa_data/tokens/box_oauth.json")
If (success <> True) Then
    System.DebugLog(tokens.LastErrorText)
    Return
End If

System.DebugLog("Authorization complete; token response saved in the protected local token file.")