PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Sb
oleobject loo_JsonOAuth2
oleobject loo_HttpCurl
oleobject loo_ResponseJson
integer li_StatusCode
li_Success = 0
// 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"
loo_Sb = create oleobject
li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
destroy loo_Sb
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Sb.AppendLn("curl -X GET ~"https://graph.microsoft.com/v1.0/sites/{{sharepoint_hostname}}:/sites/{{site_name}}~" ~")
loo_Sb.AppendLn(" -H ~"Authorization: Bearer ACCESS_TOKEN~" ~")
loo_Sb.AppendLn(" -H ~"Accept: application/json~"")
// Build the JSON that provides information for getting the OAuth2 access token using the OAuth2 client credentials flow.
loo_JsonOAuth2 = create oleobject
li_rc = loo_JsonOAuth2.ConnectToNewObject("Chilkat.JsonObject")
loo_JsonOAuth2.UpdateString("oauth2.client_id","CLIENT_ID")
loo_JsonOAuth2.UpdateString("oauth2.client_secret","CLIENT_SECRET")
loo_JsonOAuth2.UpdateString("oauth2.scope","https://graph.microsoft.com/.default")
loo_JsonOAuth2.UpdateString("oauth2.token_endpoint","https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token")
loo_HttpCurl = create oleobject
li_rc = loo_HttpCurl.ConnectToNewObject("Chilkat.HttpCurl")
// 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.
loo_HttpCurl.SetAuth(loo_JsonOAuth2)
// 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:
loo_HttpCurl.SetVar("sharepoint_hostname","example.sharepoint.com")
loo_HttpCurl.SetVar("site_name","test")
// Run the curl command.
li_Success = loo_HttpCurl.DoYourThing(loo_Sb.GetAsString())
if li_Success = 0 then
Write-Debug loo_HttpCurl.LastErrorText
destroy loo_Sb
destroy loo_JsonOAuth2
destroy loo_HttpCurl
return
end if
loo_ResponseJson = create oleobject
li_rc = loo_ResponseJson.ConnectToNewObject("Chilkat.JsonObject")
loo_ResponseJson.EmitCompact = 0
loo_HttpCurl.GetResponseJson(loo_ResponseJson)
li_StatusCode = loo_HttpCurl.StatusCode
Write-Debug "response status code: " + string(li_StatusCode)
Write-Debug loo_ResponseJson.Emit()
destroy loo_Sb
destroy loo_JsonOAuth2
destroy loo_HttpCurl
destroy loo_ResponseJson