Swift Requires Chilkat v11.5.0+
Swift
Box: Refresh and Save Rotated OAuth Tokens
See more Box Examples
Refresh the user token saved by the authorization example. Box refresh tokens are single-use and expire after 60 days. Serialize refreshes for a token pair and save the replacement immediately; do not run concurrent refresh jobs.
See the Box API guide for setup and the complete workflow.
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
// See Global Unlock Sample.
// Guide: Box API Guide.
success = false
let tokens = CkoJsonObject()!
success = tokens.loadFile(path: "qa_data/tokens/box_oauth.json")
if success != true {
print("\(tokens.lastErrorText!)")
return
}
let oauth2 = CkoOAuth2()!
oauth2.tokenEndpoint = "https://api.box.com/oauth2/token"
oauth2.clientId = "BOX_CLIENT_ID"
oauth2.clientSecret = "BOX_CLIENT_SECRET"
oauth2.refreshToken = tokens.string(of: "refresh_token")
success = oauth2.refreshAccessToken()
if success != true {
print("\(oauth2.lastErrorText!)")
return
}
success = tokens.load(json: oauth2.accessTokenResponse)
if success != true {
print("\(tokens.lastErrorText!)")
return
}
// The previous refresh token is now invalid. Preserve this response even if
// your normal token-store write fails. Do not retry with the old refresh token.
success = tokens.writeFile(path: "qa_data/tokens/box_oauth.json")
if success != true {
print("\(tokens.lastErrorText!)")
return
}
print("New access and refresh tokens saved. Do not log their values.")
}