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

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoTokens
    Handle hoOauth2
    String sTemp1

    Move False To iSuccess

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

    Move False To iSuccess

    Get Create (RefClass(cComChilkatJsonObject)) To hoTokens
    If (Not(IsComObjectCreated(hoTokens))) Begin
        Send CreateComObject of hoTokens
    End
    Get ComLoadFile Of hoTokens "qa_data/tokens/box_oauth.json" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoTokens To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatOAuth2)) To hoOauth2
    If (Not(IsComObjectCreated(hoOauth2))) Begin
        Send CreateComObject of hoOauth2
    End
    Set ComTokenEndpoint Of hoOauth2 To "https://api.box.com/oauth2/token"
    Set ComClientId Of hoOauth2 To "BOX_CLIENT_ID"
    Set ComClientSecret Of hoOauth2 To "BOX_CLIENT_SECRET"
    Get ComStringOf Of hoTokens "refresh_token" To sTemp1
    Set ComRefreshToken Of hoOauth2 To sTemp1
    Get ComRefreshAccessToken Of hoOauth2 To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoOauth2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComAccessTokenResponse Of hoOauth2 To sTemp1
    Get ComLoad Of hoTokens sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoTokens To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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.
    Get ComWriteFile Of hoTokens "qa_data/tokens/box_oauth.json" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoTokens To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "New access and refresh tokens saved. Do not log their values."


End_Procedure