Unicode C
Unicode C
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 Unicode C Downloads
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpCurlW.h>
void ChilkatSample(void)
{
BOOL success;
HCkStringBuilderW sb;
HCkJsonObjectW jsonOAuth2;
HCkHttpCurlW httpCurl;
HCkJsonObjectW responseJson;
int statusCode;
success = FALSE;
// 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 = CkStringBuilderW_Create();
CkStringBuilderW_AppendLn(sb,L"curl -X GET \"https://graph.microsoft.com/v1.0/sites/{{sharepoint_hostname}}:/sites/{{site_name}}\" \\");
CkStringBuilderW_AppendLn(sb,L" -H \"Authorization: Bearer ACCESS_TOKEN\" \\");
CkStringBuilderW_AppendLn(sb,L" -H \"Accept: application/json\"");
// Build the JSON that provides information for getting the OAuth2 access token using the OAuth2 client credentials flow.
jsonOAuth2 = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(jsonOAuth2,L"oauth2.client_id",L"CLIENT_ID");
CkJsonObjectW_UpdateString(jsonOAuth2,L"oauth2.client_secret",L"CLIENT_SECRET");
CkJsonObjectW_UpdateString(jsonOAuth2,L"oauth2.scope",L"https://graph.microsoft.com/.default");
CkJsonObjectW_UpdateString(jsonOAuth2,L"oauth2.token_endpoint",L"https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token");
httpCurl = CkHttpCurlW_Create();
// 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.
CkHttpCurlW_SetAuth(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:
CkHttpCurlW_SetVar(httpCurl,L"sharepoint_hostname",L"example.sharepoint.com");
CkHttpCurlW_SetVar(httpCurl,L"site_name",L"test");
// Run the curl command.
success = CkHttpCurlW_DoYourThing(httpCurl,CkStringBuilderW_getAsString(sb));
if (success == FALSE) {
wprintf(L"%s\n",CkHttpCurlW_lastErrorText(httpCurl));
CkStringBuilderW_Dispose(sb);
CkJsonObjectW_Dispose(jsonOAuth2);
CkHttpCurlW_Dispose(httpCurl);
return;
}
responseJson = CkJsonObjectW_Create();
CkJsonObjectW_putEmitCompact(responseJson,FALSE);
CkHttpCurlW_GetResponseJson(httpCurl,responseJson);
statusCode = CkHttpCurlW_getStatusCode(httpCurl);
wprintf(L"response status code: %d\n",statusCode);
wprintf(L"%s\n",CkJsonObjectW_emit(responseJson));
CkStringBuilderW_Dispose(sb);
CkJsonObjectW_Dispose(jsonOAuth2);
CkHttpCurlW_Dispose(httpCurl);
CkJsonObjectW_Dispose(responseJson);
}