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

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

 

 

 

(Unicode C) SharePoint -- Get Server Form Digest Value

Demonstrates how to get a server form digest value to be placed in the X-RequestDigest HTTP request header for POST, PUT, MERGE, and DELETE requests. A form digest value is typically valid for 1800 seconds (i.e. 30 minutes). This example persists the value to a file, and only requests a new form digest value if the existing one is near expiration.

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_CkFileAccessW.h>
#include <C_CkXmlW.h>
#include <C_CkDateTimeW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    HCkFileAccessW fac;
    HCkXmlW xml;
    BOOL success;
    HCkDateTimeW dtExpire;
    HCkDateTimeW dtNow;
    const wchar_t *formDigestXmlFile;
    int tNow;
    int tExpire;
    HCkHttpW http;
    const wchar_t *savedAccept;
    HCkHttpResponseW resp;
    HCkXmlW xml2;
    int timeoutInSec;

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

    // First, let's see if we already have a persisted form digest value
    // that hasn't yet expired.
    fac = CkFileAccessW_Create();
    xml = CkXmlW_Create();

    // My example code (below) persists the form digest XML in this format:
    // 
    // 	<savedFormDigestValue>
    // 	<d:ExpireDateTime>2017-04-12T20:46:39Z</d:ExpireDateTime>
    // 	<d:FormDigestValue>0x3059FFB920651834540F3E6792EA73F5746B302E953FF4E808E485DB1E6C2836C7CF924644995F092453B02A94DE14A7962674B7B16780AF16EAFB8C246BCDC7,12 Apr 2017 17:08:22 -0000</d:FormDigestValue>
    // 	</savedFormDigestValue>
    // 

    dtExpire = CkDateTimeW_Create();
    dtNow = CkDateTimeW_Create();

    formDigestXmlFile = L"qa_data/sharepoint/savedFormDigestValue.xml";
    if (CkFileAccessW_FileExists(fac,formDigestXmlFile) == TRUE) {

        CkXmlW_LoadXmlFile(xml,formDigestXmlFile);

        // Get the expire date/time
        CkDateTimeW_SetFromTimestamp(dtExpire,CkXmlW_getChildContent(xml,L"d:ExpireDateTime"));

        // Get the current date/time
        CkDateTimeW_SetFromCurrentSystemTime(dtNow);

        // Get both times as Unix time values
        tNow = CkDateTimeW_GetAsUnixTime(dtNow,FALSE);
        tExpire = CkDateTimeW_GetAsUnixTime(dtExpire,FALSE);

        // If tNow >= tExpire, then fall through.
        // Otherwise, just use the cached digest value.
        if (tNow < tExpire) {
            wprintf(L"Cached digest value is not yet expired.\n");
            wprintf(L"X-RequestDigest: %s\n",CkXmlW_getChildContent(xml,L"d:FormDigestValue"));
            CkFileAccessW_Dispose(fac);
            CkXmlW_Dispose(xml);
            CkDateTimeW_Dispose(dtExpire);
            CkDateTimeW_Dispose(dtNow);
            return;
        }

    }

    // If we got to this point, the cached digest value either does not exist, or expired.

    http = CkHttpW_Create();

    // If SharePoint Windows classic authentication is used, then set the 
    // Login, Password, LoginDomain, and NtlmAuth properties.
    CkHttpW_putLogin(http,L"SHAREPOINT_USERNAME");
    CkHttpW_putPassword(http,L"SHAREPOINT_PASSWORD");
    CkHttpW_putLoginDomain(http,L"SHAREPOINT_NTLM_DOMAIN");
    CkHttpW_putNtlmAuth(http,TRUE);

    // The more common case is to use SharePoint Online authentication (via the SPOIDCRL cookie).
    // If so, do not set Login, Password, LoginDomain, and NtlmAuth, and instead
    // establish the cookie as shown at SharePoint Online Authentication

    // When creating, updating, and deleting SharePoint entities, we'll need
    // to first get the server's form digest value to send in the X-RequestDigest header.
    // This can be retrieved by making a POST request with an empty body to
    // http://<site url>/_api/contextinfo and extracting the value of the
    // d:FormDigestValue node in the XML that the contextinfo endpoint returns.

    // Apparently, SharePoint needs an "Accept" request header equal to "application/xml",
    // otherwise SharePoint will return an utterly incomprehensible and useless error message.
    savedAccept = CkHttpW_ck_accept(http);
    CkHttpW_putAccept(http,L"application/xml");

    // Note: The last argument ("utf-8") is meaningless here because the body is empty.
    resp = CkHttpW_PostXml(http,L"https://SHAREPOINT_HTTPS_DOMAIN/_api/contextinfo",L"",L"utf-8");
    if (CkHttpW_getLastMethodSuccess(http) != TRUE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkFileAccessW_Dispose(fac);
        CkXmlW_Dispose(xml);
        CkDateTimeW_Dispose(dtExpire);
        CkDateTimeW_Dispose(dtNow);
        CkHttpW_Dispose(http);
        return;
    }

    // Restore the default Accept header
    CkHttpW_putAccept(http,savedAccept);

    if (CkHttpResponseW_getStatusCode(resp) != 200) {
        // A response status code not equal to 200 indicates failure.
        wprintf(L"Response status code = %d\n",CkHttpResponseW_getStatusCode(resp));
        wprintf(L"Response body:\n");
        wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));
        CkHttpResponseW_Dispose(resp);
        CkFileAccessW_Dispose(fac);
        CkXmlW_Dispose(xml);
        CkDateTimeW_Dispose(dtExpire);
        CkDateTimeW_Dispose(dtNow);
        CkHttpW_Dispose(http);
        return;
    }

    CkXmlW_LoadXml(xml,CkHttpResponseW_bodyStr(resp));
    CkHttpResponseW_Dispose(resp);

    // The response XML looks like this:

    // <?xml version="1.0" encoding="utf-8" ?>
    // <d:GetContextWebInformation xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" m:type="SP.ContextWebInformation">
    //     <d:FormDigestTimeoutSeconds m:type="Edm.Int32">1800</d:FormDigestTimeoutSeconds>
    //     <d:FormDigestValue>0x3059FFB920651834540F3E6792EA73F5746B302E953FF4E808E485DB1E6C2836C7CF924644995F092453B02A94DE14A7962674B7B16780AF16EAFB8C246BCDC7,12 Apr 2017 17:08:22 -0000</d:FormDigestValue>
    //     <d:LibraryVersion>15.0.4569.1000</d:LibraryVersion>
    //     <d:SiteFullUrl>https://SHAREPOINT_HTTPS_DOMAIN</d:SiteFullUrl>
    //     <d:SupportedSchemaVersions m:type="Collection(Edm.String)">
    //         <d:element>14.0.0.0</d:element>
    //         <d:element>15.0.0.0</d:element>
    //     </d:SupportedSchemaVersions>
    //     <d:WebFullUrl>https://SHAREPOINT_HTTPS_DOMAIN</d:WebFullUrl>
    // </d:GetContextWebInformation>
    // 

    // Cache the digest value, and also an expiration time.  If this code is run again
    // before the digest expires, we'll just get it from the file.
    xml2 = CkXmlW_Create();

    CkXmlW_putTag(xml2,L"savedFormDigestValue");
    CkXmlW_NewChild2(xml2,L"d:FormDigestValue",CkXmlW_getChildContent(xml,L"d:FormDigestValue"));

    timeoutInSec = CkXmlW_GetChildIntValue(xml,L"d:FormDigestTimeoutSeconds");
    wprintf(L"Timeout in seconds = %d\n",timeoutInSec);

    // Convert this to an expire timestamp.
    // Let's make it expire 30 seconds prior to the actual timeout, just to be safe.
    if (timeoutInSec > 30) {
        timeoutInSec = timeoutInSec - 30;
    }

    CkDateTimeW_SetFromCurrentSystemTime(dtExpire);
    CkDateTimeW_AddSeconds(dtExpire,timeoutInSec);
    CkXmlW_NewChild2(xml2,L"d:ExpireDateTime",CkDateTimeW_getAsTimestamp(dtExpire,FALSE));

    // Persist the digest and expire time to a file.
    CkXmlW_SaveXml(xml2,formDigestXmlFile);

    wprintf(L"Here is the new form digest value:\n");
    wprintf(L"X-RequestDigest: %s\n",CkXmlW_getChildContent(xml,L"d:FormDigestValue"));


    CkFileAccessW_Dispose(fac);
    CkXmlW_Dispose(xml);
    CkDateTimeW_Dispose(dtExpire);
    CkDateTimeW_Dispose(dtNow);
    CkHttpW_Dispose(http);
    CkXmlW_Dispose(xml2);

    }

 

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