Sample code for 30+ languages & platforms
DataFlex

MYOB Extend Refresh Access Token

See more MYOB Examples

Refreshes the access token for MYOB apps.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoJsonToken
    Handle hoOauth2
    Variant vSbJson
    Handle hoSbJson
    String sTemp1

    Move False To iSuccess

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // See Get MYOB Access Token 
    // for sample code showing how to obtain a MYOB access token for desktop apps.

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonToken
    If (Not(IsComObjectCreated(hoJsonToken))) Begin
        Send CreateComObject of hoJsonToken
    End
    Get ComLoadFile Of hoJsonToken "qa_data/tokens/myob.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Failed to load myob.json"
        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://secure.myob.com/oauth2/v1/authorize/"

    // Replace these with actual values.
    Set ComClientId Of hoOauth2 To "CLIENT_ID"
    Set ComClientSecret Of hoOauth2 To "CLIENT_SECRET"

    // Get the "refresh_token"
    Get ComStringOf Of hoJsonToken "refresh_token" To sTemp1
    Set ComRefreshToken Of hoOauth2 To sTemp1

    // Send the HTTP POST to refresh the access token..
    Set ComVerboseLogging Of hoOauth2 To True
    Get ComRefreshAccessToken Of hoOauth2 To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoOauth2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Load the access token response into the json object 
    Get ComAccessTokenResponse Of hoOauth2 To sTemp1
    Get ComLoad Of hoJsonToken sTemp1 To iSuccess

    // Save the new JSON access token response to a file.
    // The access + refresh tokens contained in this JSON will be needed for the next refresh.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJson
    If (Not(IsComObjectCreated(hoSbJson))) Begin
        Send CreateComObject of hoSbJson
    End
    Set ComEmitCompact Of hoJsonToken To False
    Get pvComObject of hoSbJson to vSbJson
    Get ComEmitSb Of hoJsonToken vSbJson To iSuccess
    Get ComWriteFile Of hoSbJson "qa_data/tokens/myob.json" "utf-8" False To iSuccess

    Showln "OAuth2 authorization granted!"
    Get ComAccessToken Of hoOauth2 To sTemp1
    Showln "New Access Token = " sTemp1


End_Procedure