Sample code for 30+ languages & platforms
DataFlex

Refresh eBay Application Access Token

See more eBay Examples

This example shows how to get a new eBay REST API access token when the original is close to expiration.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoJson
    Handle hoOauth2
    String sTemp1

    Move False To iSuccess

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

    // The previously fetched eBay access token looks like this:

    // {
    //   "access_token": "v^1.1#i^1# ... HwSAAA=",
    //   "expires_in": 7200,
    //   "refresh_token": "v^1.1#i^1# ... FXjEyODQ=",
    //   "refresh_token_expires_in": 47304000,
    //   "token_type": "User Access Token"
    // }

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComLoadFile Of hoJson "qa_data/tokens/ebay-access-token.json" To iSuccess
    If (iSuccess = False) Begin
        Showln "Failed to load the existing ebay access token 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://api.sandbox.ebay.com/identity/v1/oauth2/token"

    // Replace these with actual values.
    Set ComClientId Of hoOauth2 To "EBAY_CLIENT_ID"
    Set ComClientSecret Of hoOauth2 To "EBAY_CLIENT_SECRET"

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

    // Send the HTTP POST to refresh the access token..
    Get ComRefreshAccessToken Of hoOauth2 To iSuccess
    If (iSuccess = False) 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 hoJson 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.
    Set ComEmitCompact Of hoJson To False
    Get ComWriteFile Of hoJson "qa_data/tokens/ebay-access-token.json" To iSuccess

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


End_Procedure