DataFlex
DataFlex
curl with OAuth2 Client Credentials
See more CURL Examples
This example shows how to run a simple CURL command with an OAuth2 access token for authorization. We use CURL to retrieve a SharePoint site ID, and Chilkat automatically fetches the OAuth2 access token using the provided credentials.Chilkat DataFlex Downloads
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
Get ComUpdateString Of hoJsonOAuth2 "oauth2.client_id" "CLIENT_ID" To iSuccess
Get ComUpdateString Of hoJsonOAuth2 "oauth2.client_secret" "CLIENT_SECRET" To iSuccess
Get ComUpdateString Of hoJsonOAuth2 "oauth2.scope" "https://graph.microsoft.com/.default" To iSuccess
Get ComUpdateString Of hoJsonOAuth2 "oauth2.token_endpoint" "https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token" 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