Sample code for 30+ languages & platforms
DataFlex

X.com OAuth 2.0 Refresh Access Token

See more X Examples

Demonstrates how to get an X.com OAuth2 access token from a desktop application or script.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoJsonToken
    Handle hoOauth2
    Handle hoDtExpire
    Variant vSbJson
    Handle hoSbJson
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonToken
    If (Not(IsComObjectCreated(hoJsonToken))) Begin
        Send CreateComObject of hoJsonToken
    End
    Get ComLoadFile Of hoJsonToken "qa_data/tokens/x.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Failed to load x.json"
        Procedure_Return
    End

    // The access token JSON looks something like this:

    // {
    //   "token_type": "bearer",
    //   "expires_in": 7200,
    //   "access_token": "VmNDLVZiYUZwejY5Mkx3RblFTmo3ek1leTRGclMuZFVOUTVUTWpNbVZKb1N5OjE3NDAxNjk4MDVxNTE6MToxOaF0OjE",
    //   "scope": "block.read follows.read offline.access tweet.write block.write like.write like.read users.read tweet.read follows.write",
    //   "refresh_token": "eThOWVVFSTRqdVp3QTFndGsxM2ZGZmFyNWVrU0phM1JKbGdrbGNWRGJzb1loOjE3NDAwMjk4MDUxNTI6MTowOnJ0OjE"
    // }

    Get Create (RefClass(cComChilkatOAuth2)) To hoOauth2
    If (Not(IsComObjectCreated(hoOauth2))) Begin
        Send CreateComObject of hoOauth2
    End

    Set ComTokenEndpoint Of hoOauth2 To "https://api.x.com/2/oauth2/token"

    // Replace this with your actual X.com OAuth2 Client ID and Client Secret
    Set ComClientId Of hoOauth2 To "OAUTH2_CLIENT_ID"
    Set ComClientSecret Of hoOauth2 To "OAUTH2_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..
    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

    // If an "expires_on" member does not exist, then add the JSON member by
    // getting the current system date/time and adding the "expires_in" seconds.
    // This way we'll know when the token expires.
    Get ComHasMember Of hoJsonToken "expires_on" To bTemp1
    If (bTemp1 = False) Begin
        Get Create (RefClass(cComCkDateTime)) To hoDtExpire
        If (Not(IsComObjectCreated(hoDtExpire))) Begin
            Send CreateComObject of hoDtExpire
        End
        Get ComSetFromCurrentSystemTime Of hoDtExpire To iSuccess
        Get ComIntOf Of hoJsonToken "expires_in" To iTemp1
        Get ComAddSeconds Of hoDtExpire iTemp1 To iSuccess
        Get ComGetAsUnixTimeStr Of hoDtExpire False To sTemp1
        Get ComAppendString Of hoJsonToken "expires_on" sTemp1 To iSuccess
    End

    // 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/x.json" "utf-8" False To iSuccess

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


End_Procedure