Go Requires Chilkat v11.5.0+
Go
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 Go Downloads
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
oauth2 := chilkat.NewOAuth2()
oauth2.SetListenPort(3017)
oauth2.SetAuthorizationEndpoint("https://account.box.com/api/oauth2/authorize")
oauth2.SetTokenEndpoint("https://api.box.com/oauth2/token")
oauth2.SetClientId("BOX_CLIENT_ID")
oauth2.SetClientSecret("BOX_CLIENT_SECRET")
// Use Box's documented confidential-client authorization-code flow.
oauth2.SetCodeChallenge(false)
url := oauth2.StartAuth()
if oauth2.LastMethodSuccess() != true {
fmt.Println(oauth2.LastErrorText())
oauth2.DisposeOAuth2()
return
}
success = oauth2.LaunchBrowser(*url)
if success != true {
fmt.Println(oauth2.LastErrorText())
oauth2.DisposeOAuth2()
return
}
elapsedMs := 0
for (elapsedMs < 120000) && (oauth2.AuthFlowState() < 3) {
oauth2.SleepMs(100)
elapsedMs = elapsedMs + 100
}
if oauth2.AuthFlowState() < 3 {
oauth2.Cancel()
fmt.Println("Authorization timed out.")
oauth2.DisposeOAuth2()
return
}
if oauth2.AuthFlowState() != 3 {
fmt.Println("Authorization was denied or failed. State: ", oauth2.AuthFlowState())
oauth2.DisposeOAuth2()
return
}
// Retain both tokens in your application's protected token store.
// The variables below are deliberately not printed.
accessToken := oauth2.AccessToken()
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.
tokens := chilkat.NewJsonObject()
success = tokens.Load(oauth2.AccessTokenResponse())
if success != true {
fmt.Println(tokens.LastErrorText())
oauth2.DisposeOAuth2()
tokens.DisposeJsonObject()
return
}
success = tokens.WriteFile("qa_data/tokens/box_oauth.json")
if success != true {
fmt.Println(tokens.LastErrorText())
oauth2.DisposeOAuth2()
tokens.DisposeJsonObject()
return
}
fmt.Println("Authorization complete; token response saved in the protected local token file.")
oauth2.DisposeOAuth2()
tokens.DisposeJsonObject()