Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Unicode C Web API Examples

Primary Categories

ABN AMRO
AWS Translate
Activix CRM
Adyen
Amazon MWS
Aruba Fatturazione
Azure Maps
Azure Monitor
Azure OAuth2
Azure Storage Accounts
Bitfinex v2 REST
Bluzone
CardConnect
ClickBank
Clickatell
Cloudfare
DocuSign
ETrade
Etsy
Facebook
GeoOp
GetHarvest
Global Payments
Google People
Google Search Console
Hungary NAV Invoicing
IBM Text to Speech
Jira
MYOB
Magento

MercadoLibre
Microsoft Calendar
Microsoft Group
Microsoft Tasks and Plans
Okta OAuth/OIDC
OneLogin OIDC
PayPal
Peoplevox
Populi
QuickBooks
Rabobank
SOAP finkok.com
Shippo
Shopify
SimpleTexting
Square
Stripe
SugarCRM
Trello
Twilio
Twitter
VoiceBase
Vonage
Walmart
Walmart v3
WhatsApp
WooCommerce
Xero
Yousign
_Miscellaneous_
eBay
effectconnect

 

 

 

(Unicode C) Docusign JSON Web Token (JWT) Grant

Demonstrates how to obtain an access token using the JSON Web Token (JWT) Grant. This is good for service integrations where authorization and authentication is automated and cannot have interactive Docusign account owner interaction. Consent for the access is obtained beforehand in various ways. See Obtaining Consent.

For more information, see https://developers.docusign.com/esign-rest-api/guides/authentication/oauth2-jsonwebtoken

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++

Win Mobile 5.0/Pocket PC 2003

Solaris C/C++

FreeBSD C/C++

OpenBSD C/C++

MinGW C/C++

#include <C_CkPrivateKeyW.h>
#include <C_CkJwtW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpRequestW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    HCkPrivateKeyW privKey;
    BOOL success;
    HCkJwtW jwt;
    HCkJsonObjectW jose;
    HCkJsonObjectW claims;
    int curDateTime;
    const wchar_t *token;
    HCkHttpW http;
    HCkHttpRequestW req;
    HCkHttpResponseW resp;

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

    // 1. Assume we have already requested and obtained application consent.
    //    (See Request Docusign Application Consent
    // 
    // 2. Create a JWT Token.
    // 
    privKey = CkPrivateKeyW_Create();

    // Load an RSA private key from a PEM file.
    success = CkPrivateKeyW_LoadPemFile(privKey,L"qa_data/pem/docusign_private_rsa_key.pem");
    if (success != TRUE) {
        wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(privKey));
        CkPrivateKeyW_Dispose(privKey);
        return;
    }

    jwt = CkJwtW_Create();

    // Build the JOSE header
    jose = CkJsonObjectW_Create();
    // Use RS256.  Pass the string "RS384" or "RS512" to use RSA with SHA-384 or SHA-512.
    success = CkJsonObjectW_AppendString(jose,L"alg",L"RS256");
    success = CkJsonObjectW_AppendString(jose,L"typ",L"JWT");

    // Now build the JWT claims (also known as the payload)
    claims = CkJsonObjectW_Create();

    // Replace these with actual values.
    // The client ID is also known as the "integration key" in Docusign.
    success = CkJsonObjectW_AppendString(claims,L"iss",L"MY_DOCUSIGN_CLIENT_ID");
    // In your DocuSign Admin/Account/UserProfile, this is the API Username, such as 14612117-2530-4982-8c49-ba8766303272
    success = CkJsonObjectW_AppendString(claims,L"sub",L"DOCUSIGN_USER_ID");
    success = CkJsonObjectW_AppendString(claims,L"aud",L"account-d.docusign.com");
    success = CkJsonObjectW_AppendString(claims,L"scope",L"signature");

    // Set the timestamp of when the JWT was created to now.
    curDateTime = CkJwtW_GenNumericDate(jwt,0);
    success = CkJsonObjectW_AddIntAt(claims,-1,L"iat",curDateTime);

    // Set the "not process before" timestamp to now.
    success = CkJsonObjectW_AddIntAt(claims,-1,L"nbf",curDateTime);

    // Set the timestamp defining an expiration time (end time) for the token
    // to be now + 1 hour (3600 seconds)
    success = CkJsonObjectW_AddIntAt(claims,-1,L"exp",curDateTime + 3600);

    // Produce the smallest possible JWT:
    CkJwtW_putAutoCompact(jwt,TRUE);

    // Create the JWT token.  This is where the RSA signature is created.
    token = CkJwtW_createJwtPk(jwt,CkJsonObjectW_emit(jose),CkJsonObjectW_emit(claims),privKey);

    wprintf(L"%s\n",token);

    // Do the following CURL statement to get the response JSON which contains the access token.
    // curl --data "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=YOUR_JSON_WEB_TOKEN" 
    //      --request POST https://account-d.docusign.com/oauth/token
    http = CkHttpW_Create();

    req = CkHttpRequestW_Create();
    CkHttpRequestW_AddParam(req,L"grant_type",L"urn:ietf:params:oauth:grant-type:jwt-bearer");
    CkHttpRequestW_AddParam(req,L"assertion",token);

    resp = CkHttpW_PostUrlEncoded(http,L"https://account-d.docusign.com/oauth/token",req);
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkPrivateKeyW_Dispose(privKey);
        CkJwtW_Dispose(jwt);
        CkJsonObjectW_Dispose(jose);
        CkJsonObjectW_Dispose(claims);
        CkHttpW_Dispose(http);
        CkHttpRequestW_Dispose(req);
        return;
    }

    wprintf(L"response status = %d\n",CkHttpResponseW_getStatusCode(resp));
    if (CkHttpResponseW_getStatusCode(resp) != 200) {
        wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));
        wprintf(L"Failed.\n");
    }
    else {
        // Save the access token to a file for use in subsequent requests..
        // (Or you may simply persiste the access token in memory for your applicaton to use for subsequent REST API calls..)
        success = CkHttpResponseW_SaveBodyText(resp,TRUE,L"qa_data/tokens/docusign.json");
        wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));
        wprintf(L"Success.\n");
    }

    CkHttpResponseW_Dispose(resp);


    CkPrivateKeyW_Dispose(privKey);
    CkJwtW_Dispose(jwt);
    CkJsonObjectW_Dispose(jose);
    CkJsonObjectW_Dispose(claims);
    CkHttpW_Dispose(http);
    CkHttpRequestW_Dispose(req);

    }

 

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