PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttpCurl.pb"
Procedure ChilkatExample()
success.i = 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"
sb.i = CkStringBuilder::ckCreate()
If sb.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppendLn(sb,"curl -X GET " + Chr(34) + "https://graph.microsoft.com/v1.0/sites/{{sharepoint_hostname}}:/sites/{{site_name}}" + Chr(34) + " \")
CkStringBuilder::ckAppendLn(sb," -H " + Chr(34) + "Authorization: Bearer ACCESS_TOKEN" + Chr(34) + " \")
CkStringBuilder::ckAppendLn(sb," -H " + Chr(34) + "Accept: application/json" + Chr(34))
; Build the JSON that provides information for getting the OAuth2 access token using the OAuth2 client credentials flow.
jsonOAuth2.i = CkJsonObject::ckCreate()
If jsonOAuth2.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(jsonOAuth2,"oauth2.client_id","CLIENT_ID")
CkJsonObject::ckUpdateString(jsonOAuth2,"oauth2.client_secret","CLIENT_SECRET")
CkJsonObject::ckUpdateString(jsonOAuth2,"oauth2.scope","https://graph.microsoft.com/.default")
CkJsonObject::ckUpdateString(jsonOAuth2,"oauth2.token_endpoint","https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token")
httpCurl.i = CkHttpCurl::ckCreate()
If httpCurl.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; 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.
CkHttpCurl::ckSetAuth(httpCurl,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:
CkHttpCurl::ckSetVar(httpCurl,"sharepoint_hostname","example.sharepoint.com")
CkHttpCurl::ckSetVar(httpCurl,"site_name","test")
; Run the curl command.
success = CkHttpCurl::ckDoYourThing(httpCurl,CkStringBuilder::ckGetAsString(sb))
If success = 0
Debug CkHttpCurl::ckLastErrorText(httpCurl)
CkStringBuilder::ckDispose(sb)
CkJsonObject::ckDispose(jsonOAuth2)
CkHttpCurl::ckDispose(httpCurl)
ProcedureReturn
EndIf
responseJson.i = CkJsonObject::ckCreate()
If responseJson.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::setCkEmitCompact(responseJson, 0)
CkHttpCurl::ckGetResponseJson(httpCurl,responseJson)
statusCode.i = CkHttpCurl::ckStatusCode(httpCurl)
Debug "response status code: " + Str(statusCode)
Debug CkJsonObject::ckEmit(responseJson)
CkStringBuilder::ckDispose(sb)
CkJsonObject::ckDispose(jsonOAuth2)
CkHttpCurl::ckDispose(httpCurl)
CkJsonObject::ckDispose(responseJson)
ProcedureReturn
EndProcedure