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

AutoIt
Local $bSuccess = False

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

$bSuccess = False

$oOauth2 = ObjCreate("Chilkat.OAuth2")
$oOauth2.ListenPort = 3017
$oOauth2.AuthorizationEndpoint = "https://account.box.com/api/oauth2/authorize"
$oOauth2.TokenEndpoint = "https://api.box.com/oauth2/token"
$oOauth2.ClientId = "BOX_CLIENT_ID"
$oOauth2.ClientSecret = "BOX_CLIENT_SECRET"
;  Use Box's documented confidential-client authorization-code flow.
$oOauth2.CodeChallenge = False
Local $sUrl = $oOauth2.StartAuth()
If ($oOauth2.LastMethodSuccess <> True) Then
    ConsoleWrite($oOauth2.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oOauth2.LaunchBrowser($sUrl)
If ($bSuccess <> True) Then
    ConsoleWrite($oOauth2.LastErrorText & @CRLF)
    Exit
EndIf

Local $iElapsedMs = 0
While ($iElapsedMs < 120000) And ($oOauth2.AuthFlowState < 3)
    $oOauth2.SleepMs 100
    $iElapsedMs = $iElapsedMs + 100
Wend
If ($oOauth2.AuthFlowState < 3) Then
    $oOauth2.Cancel()
    ConsoleWrite("Authorization timed out." & @CRLF)
    Exit
EndIf

If ($oOauth2.AuthFlowState <> 3) Then
    ConsoleWrite("Authorization was denied or failed. State: " & $oOauth2.AuthFlowState & @CRLF)
    Exit
EndIf

;  Retain both tokens in your application's protected token store.
;  The variables below are deliberately not printed.
Local $sAccessToken = $oOauth2.AccessToken
Local $sRefreshToken = $oOauth2.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.
$oTokens = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oTokens.Load($oOauth2.AccessTokenResponse)
If ($bSuccess <> True) Then
    ConsoleWrite($oTokens.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oTokens.WriteFile("qa_data/tokens/box_oauth.json")
If ($bSuccess <> True) Then
    ConsoleWrite($oTokens.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("Authorization complete; token response saved in the protected local token file." & @CRLF)