Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

C Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(C) Azure Key Vault Get OAuth2 Access Token using Client Credentials

See more Azure Key Vault Examples

Demonstrates how to get an OAuth2 access token using client credentials for an Azure Key Vault resource.

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Solaris C/C++

MinGW C/C++

#include <C_CkHttp.h>
#include <C_CkHttpRequest.h>
#include <C_CkHttpResponse.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    HCkHttp http;
    HCkHttpRequest req;
    HCkHttpResponse resp;
    const char *strRespBody;
    int respStatusCode;
    HCkJsonObject jsonResp;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // You can use OAuth2 client credentials with an Azure App (service principal) that has
    // the required Role-Based Access Control (RBAC) permissions.
    // In this case, it would be service principal with RBAC permissions to administer and manage
    // the key vault.

    // You can create the Azure App (also known as the Service Principal)
    // in the Azure CLI (command line interface) as follows:

    // ----------------------------------------------------------------------
    // az ad sp create-for-rbac --name http://example.com --role Contributor
    // ----------------------------------------------------------------------

    // The argument to --name must be a valid URI that is a verified domain of your 
    // organization or its subdomain.

    // The output of the above "az ad sp create-for-rbac ..." command is JSON such as this:

    // {
    //   "appId": "25ac6e3a-9ac7-42b9-b13e-18644c1de959",
    //   "displayName": "azure-cli-2023-10-14-22-38-15",
    //   "name": "http://example.com",
    //   "password": "f1f2f3f0-52dc-4236-8295-c8a1d6aa393c",
    //   "tenant": "4d8dfd66-68d1-13b0-af5c-b31b4b3d53d"
    // }

    // Save the values in the above JSON.  You'll need it below..

    // You'll also want to add the role of "Key Vault Administrator" to the Service Principal
    // for the particular key vault.

    // ----------------------------------------------------------------------
    // az role assignment create --assignee <Application-ID> --role "Key Vault Administrator" 
    //       --scope /subscriptions/<Subscription-ID>/resourceGroups/<Resource-Group-Name>/providers/Microsoft.KeyVault/vaults/<KeyVault-Name>
    // ----------------------------------------------------------------------

    http = CkHttp_Create();
    req = CkHttpRequest_Create();

    // Add query params to the request.
    CkHttpRequest_AddParam(req,"grant_type","client_credentials");

    // Use the service principal's appId
    CkHttpRequest_AddParam(req,"client_id","25ac6e3a-9ac7-42b9-b13e-18644c1de959");

    // Use the service principal's password.
    CkHttpRequest_AddParam(req,"client_secret","f1f2f3f0-52dc-4236-8295-c8a1d6aa393c");

    // Note: The resource must match the API for which you're using the access token..
    CkHttpRequest_AddParam(req,"resource","https://vault.azure.net");

    CkHttp_SetUrlVar(http,"tenant","4d8dfd66-68d1-13b0-af5c-b31b4b3d53d");
    resp = CkHttp_PostUrlEncoded(http,"https://login.microsoftonline.com/{$tenant}/oauth2/token",req);
    if (CkHttp_getLastMethodSuccess(http) != TRUE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkHttpRequest_Dispose(req);
        return;
    }

    strRespBody = CkHttpResponse_bodyStr(resp);
    respStatusCode = CkHttpResponse_getStatusCode(resp);
    if (respStatusCode >= 400) {
        printf("Response Status Code = %d\n",respStatusCode);
        printf("Response Body:\n");
        printf("%s\n",strRespBody);
        CkHttpResponse_Dispose(resp);
        CkHttp_Dispose(http);
        CkHttpRequest_Dispose(req);
        return;
    }

    jsonResp = CkJsonObject_Create();
    CkJsonObject_Load(jsonResp,strRespBody);
    CkJsonObject_putEmitCompact(jsonResp,FALSE);
    printf("%s\n",CkJsonObject_emit(jsonResp));

    // The result is an access token such as the following:

    // {
    //   "token_type": "Bearer",
    //   "expires_in": "3600",
    //   "ext_expires_in": "3600",
    //   "expires_on": "1557864616",
    //   "not_before": "1557860716",
    //   "resource": "https://vault.azure.net",
    //   "access_token": "eyJ0eXAiOiJKV1QiL ... 20UFDDOHEyUg"
    // }

    // If you wish, you can save the token to a file.
    // The access token is generally valid for 1 hour.
    // After 1 hour, you would need to get a new access token in the same way.
    CkJsonObject_WriteFile(jsonResp,"qa_data/tokens/azureKeyVaultToken.json");

    CkHttpResponse_Dispose(resp);


    CkHttp_Dispose(http);
    CkHttpRequest_Dispose(req);
    CkJsonObject_Dispose(jsonResp);

    }

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.