Sample code for 30+ languages & platforms
Visual FoxPro 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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loOauth2
LOCAL lcUrl
LOCAL lnElapsedMs
LOCAL lcAccessToken
LOCAL lcRefreshToken
LOCAL loTokens

lnSuccess = 0

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

lnSuccess = 0

loOauth2 = CreateObject('Chilkat.OAuth2')
loOauth2.ListenPort = 3017
loOauth2.AuthorizationEndpoint = "https://account.box.com/api/oauth2/authorize"
loOauth2.TokenEndpoint = "https://api.box.com/oauth2/token"
loOauth2.ClientId = "BOX_CLIENT_ID"
loOauth2.ClientSecret = "BOX_CLIENT_SECRET"
*  Use Box's documented confidential-client authorization-code flow.
loOauth2.CodeChallenge = 0
lcUrl = loOauth2.StartAuth()
IF (loOauth2.LastMethodSuccess <> 1) THEN
    ? loOauth2.LastErrorText
    RELEASE loOauth2
    CANCEL
ENDIF

lnSuccess = loOauth2.LaunchBrowser(lcUrl)
IF (lnSuccess <> 1) THEN
    ? loOauth2.LastErrorText
    RELEASE loOauth2
    CANCEL
ENDIF

lnElapsedMs = 0
DO WHILE (lnElapsedMs < 120000) AND (loOauth2.AuthFlowState < 3)
    loOauth2.SleepMs(100)
    lnElapsedMs = lnElapsedMs + 100
ENDDO
IF (loOauth2.AuthFlowState < 3) THEN
    loOauth2.Cancel()
    ? "Authorization timed out."
    RELEASE loOauth2
    CANCEL
ENDIF

IF (loOauth2.AuthFlowState <> 3) THEN
    ? "Authorization was denied or failed. State: " + STR(loOauth2.AuthFlowState)
    RELEASE loOauth2
    CANCEL
ENDIF

*  Retain both tokens in your application's protected token store.
*  The variables below are deliberately not printed.
lcAccessToken = loOauth2.AccessToken
lcRefreshToken = loOauth2.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.
loTokens = CreateObject('Chilkat.JsonObject')
lnSuccess = loTokens.Load(loOauth2.AccessTokenResponse)
IF (lnSuccess <> 1) THEN
    ? loTokens.LastErrorText
    RELEASE loOauth2
    RELEASE loTokens
    CANCEL
ENDIF

lnSuccess = loTokens.WriteFile("qa_data/tokens/box_oauth.json")
IF (lnSuccess <> 1) THEN
    ? loTokens.LastErrorText
    RELEASE loOauth2
    RELEASE loTokens
    CANCEL
ENDIF

? "Authorization complete; token response saved in the protected local token file."

RELEASE loOauth2
RELEASE loTokens