Sample code for 30+ languages & platforms
DataFlex

curl with use of Local Manager Secrets for Credentials

See more CURL Examples

This example demonstrates how to retrieve secrets from the local credential manager instead of embedding them directly in your source code.

On Windows, this refers to the Windows Credential Manager, and on macOS, it refers to the Apple Keychain.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSb
    Variant vJsonOAuth2
    Handle hoJsonOAuth2
    Handle hoHttpCurl
    Variant vResponseJson
    Handle hoResponseJson
    Integer iStatusCode
    String sTemp1

    Move False To iSuccess

    // This example will run the following curl command

    // curl -X GET "https://graph.microsoft.com/v1.0/sites/{{sharepoint_hostname}}:/sites/{{site_name}}" \
    //   -H "Authorization: Bearer ACCESS_TOKEN" \
    //   -H "Accept: application/json"

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
    If (Not(IsComObjectCreated(hoSb))) Begin
        Send CreateComObject of hoSb
    End
    Get ComAppendLn Of hoSb 'curl -X GET "https://graph.microsoft.com/v1.0/sites/{{sharepoint_hostname}}:/sites/{{site_name}}" \' To iSuccess
    Get ComAppendLn Of hoSb '  -H "Authorization: Bearer ACCESS_TOKEN" \' To iSuccess
    Get ComAppendLn Of hoSb '  -H "Accept: application/json"' To iSuccess

    // Build the JSON that provides information for getting the OAuth2 access token using the OAuth2 client credentials flow.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOAuth2
    If (Not(IsComObjectCreated(hoJsonOAuth2))) Begin
        Send CreateComObject of hoJsonOAuth2
    End

    // The notation "!!sharepoint|oauth2|client_id" is a secrets specification string 
    // that can have up to 4 components: !!appName|service|domain|username
    // where appName and domain are optional.
    // See Chilkat v11.5.0 — Secrets Integration
    // Also see: Chilkat Secrets API

    // The following example shows how to store (or update) the secrets used in this example
    // Storing Secrets in the Local Manager

    // We must enable secrets integration for Chilkat to auto-resolve when the string begins with "!!".
    Set ComEnableSecrets Of hoJsonOAuth2 To True
    Get ComUpdateString Of hoJsonOAuth2 "oauth2.client_id" "!!sharepoint|oauth2|client_id" To iSuccess
    Get ComUpdateString Of hoJsonOAuth2 "oauth2.client_secret" "!!sharepoint|oauth2|client_secret" To iSuccess
    Get ComUpdateString Of hoJsonOAuth2 "oauth2.scope" "https://graph.microsoft.com/.default" To iSuccess
    Get ComUpdateString Of hoJsonOAuth2 "oauth2.token_endpoint" "!!sharepoint|oauth2|token_endpoint" To iSuccess

    Get Create (RefClass(cComChilkatHttpCurl)) To hoHttpCurl
    If (Not(IsComObjectCreated(hoHttpCurl))) Begin
        Send CreateComObject of hoHttpCurl
    End

    // Provide the information for getting the OAuth2 access token from the token endpoint
    // Note: The Authorization header specified in the curl command will be ignored and replaced using the OAuth2 access token obtained at runtime from the token endpoint.
    Get pvComObject of hoJsonOAuth2 to vJsonOAuth2
    Get ComSetAuth Of hoHttpCurl vJsonOAuth2 To iSuccess

    // The placeholders {{sharepoint_hostname}} and {{site_name}} represent variables that must be defined before execution.
    // When DoYourThing runs the curl command, it automatically substitutes these placeholders with their corresponding values.
    // Below are the values assigned to these variables:
    Send ComSetVar To hoHttpCurl "sharepoint_hostname" "example.sharepoint.com"
    Send ComSetVar To hoHttpCurl "site_name" "test"

    // Run the curl command.
    Get ComGetAsString Of hoSb To sTemp1
    Get ComDoYourThing Of hoHttpCurl sTemp1 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttpCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoResponseJson
    If (Not(IsComObjectCreated(hoResponseJson))) Begin
        Send CreateComObject of hoResponseJson
    End
    Set ComEmitCompact Of hoResponseJson To False

    Get pvComObject of hoResponseJson to vResponseJson
    Get ComGetResponseJson Of hoHttpCurl vResponseJson To iSuccess

    Get ComStatusCode Of hoHttpCurl To iStatusCode
    Showln "response status code: " iStatusCode

    Get ComEmit Of hoResponseJson To sTemp1
    Showln sTemp1


End_Procedure