Sample code for 30+ languages & platforms
Unicode C

WordPress API Key Authentication with miniOrange Plugin

See more WordPress Examples

Demonstrates API key authentication using the miniOrange API Authentication plugin.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkJsonArrayW.h>
#include <C_CkDtObjW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkStringBuilderW sbResponseBody;
    HCkJsonArrayW jarrResp;
    int respStatusCode;
    HCkDtObjW date_gmt;
    HCkJsonObjectW json;
    int id;
    const wchar_t *date;
    const wchar_t *guidRendered;
    const wchar_t *modified;
    const wchar_t *modified_gmt;
    const wchar_t *slug;
    const wchar_t *status;
    const wchar_t *v_type;
    const wchar_t *link;
    const wchar_t *titleRendered;
    const wchar_t *contentRendered;
    BOOL contentProtected;
    const wchar_t *excerptRendered;
    BOOL excerptProtected;
    int author;
    int featured_media;
    const wchar_t *comment_status;
    const wchar_t *ping_status;
    BOOL sticky;
    const wchar_t *template;
    const wchar_t *format;
    int j;
    int count_j;
    int intVal;
    const wchar_t *href;
    BOOL embeddable;
    int count;
    const wchar_t *taxonomy;
    const wchar_t *name;
    BOOL templated;
    int i;
    int count_i;

    success = FALSE;

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

    http = CkHttpW_Create();

    //  Use your API key here, such as TaVFjSBu8IMR0MbvZNn7A6P04GXrbtHm
    //  This causes the "Authorization: Bearer <api_key>" header to be added to each HTTP request.
    CkHttpW_putAuthToken(http,L"api_key");

    sbResponseBody = CkStringBuilderW_Create();
    success = CkHttpW_QuickGetSb(http,L"https://www.yoursite.com/wp-json/wp/v2/posts?page=1",sbResponseBody);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(sbResponseBody);
        return;
    }

    jarrResp = CkJsonArrayW_Create();
    CkJsonArrayW_LoadSb(jarrResp,sbResponseBody);
    CkJsonArrayW_putEmitCompact(jarrResp,FALSE);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",CkJsonArrayW_emit(jarrResp));

    respStatusCode = CkHttpW_getLastStatus(http);
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkHttpW_lastHeader(http));
        wprintf(L"Failed.\n");
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonArrayW_Dispose(jarrResp);
        return;
    }

    //  Sample JSON response:
    //  (Sample code for parsing the JSON response is shown below)

    //  [
    //    {
    //      "id": 1902,
    //      "date": "2020-11-16T09:54:09",
    //      "date_gmt": "2020-11-16T16:54:09",
    //      "guid": {
    //        "rendered": "http:\/\/cknotes.com\/?p=1902"
    //      },
    //      "modified": "2020-11-16T09:54:09",
    //      "modified_gmt": "2020-11-16T16:54:09",
    //      "slug": "xero-redirect-uri-for-oauth2-and-desktop-apps",
    //      "status": "publish",
    //      "type": "post",
    //      "link": "https:\/\/cknotes.com\/xero-redirect-uri-for-oauth2-and-desktop-apps\/",
    //      "title": {
    //        "rendered": "Xero Redirect URI for OAuth2 and Desktop Apps"
    //      },
    //      "content": {
    //        "rendered": "<p>...",
    //        "protected": false
    //      },
    //      "excerpt": {
    //        "rendered": "<p>...",
    //        "protected": false
    //      },
    //      "author": 1,
    //      "featured_media": 0,
    //      "comment_status": "closed",
    //      "ping_status": "open",
    //      "sticky": false,
    //      "template": "",
    //      "format": "standard",
    //      "meta": [
    //      ],
    //      "categories": [
    //        815
    //      ],
    //      "tags": [
    //        594,
    //        816
    //      ],
    //      "_links": {
    //        "self": [
    //          {
    //            "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/1902"
    //          }
    //        ],
    //        "collection": [
    //          {
    //            "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts"
    //          }
    //        ],
    //        "about": [
    //          {
    //            "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/types\/post"
    //          }
    //        ],
    //        "author": [
    //          {
    //            "embeddable": true,
    //            "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/users\/1"
    //          }
    //        ],
    //        "replies": [
    //          {
    //            "embeddable": true,
    //            "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/comments?post=1902"
    //          }
    //        ],
    //        "version-history": [
    //          {
    //            "count": 1,
    //            "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/1902\/revisions"
    //          }
    //        ],
    //        "predecessor-version": [
    //          {
    //            "id": 1904,
    //            "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/1902\/revisions\/1904"
    //          }
    //        ],
    //        "wp:attachment": [
    //          {
    //            "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/media?parent=1902"
    //          }
    //        ],
    //        "wp:term": [
    //          {
    //            "taxonomy": "category",
    //            "embeddable": true,
    //            "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/categories?post=1902"
    //          },
    //          {
    //            "taxonomy": "post_tag",
    //            "embeddable": true,
    //            "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/tags?post=1902"
    //          }
    //        ],
    //        "curies": [
    //          {
    //            "name": "wp",
    //            "href": "https:\/\/api.w.org\/{rel}",
    //            "templated": true
    //          }
    //        ]
    //      }
    //    },
    //  ...
    //  ]

    //  Sample code for parsing the JSON response...
    //  Use the following online tool to generate parsing code from sample JSON:
    //  Generate Parsing Code from JSON

    //  Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    //  See this example explaining how this memory should be used: const char * functions.

    date_gmt = CkDtObjW_Create();

    i = 0;
    count_i = CkJsonArrayW_getSize(jarrResp);
    while (i < count_i) {
        json = CkJsonArrayW_ObjectAt(jarrResp,i);
        id = CkJsonObjectW_IntOf(json,L"id");
        date = CkJsonObjectW_stringOf(json,L"date");
        CkJsonObjectW_DtOf(json,L"date_gmt",FALSE,date_gmt);
        guidRendered = CkJsonObjectW_stringOf(json,L"guid.rendered");
        modified = CkJsonObjectW_stringOf(json,L"modified");
        modified_gmt = CkJsonObjectW_stringOf(json,L"modified_gmt");
        slug = CkJsonObjectW_stringOf(json,L"slug");
        status = CkJsonObjectW_stringOf(json,L"status");
        v_type = CkJsonObjectW_stringOf(json,L"type");
        link = CkJsonObjectW_stringOf(json,L"link");
        titleRendered = CkJsonObjectW_stringOf(json,L"title.rendered");
        contentRendered = CkJsonObjectW_stringOf(json,L"content.rendered");
        contentProtected = CkJsonObjectW_BoolOf(json,L"content.protected");
        excerptRendered = CkJsonObjectW_stringOf(json,L"excerpt.rendered");
        excerptProtected = CkJsonObjectW_BoolOf(json,L"excerpt.protected");
        author = CkJsonObjectW_IntOf(json,L"author");
        featured_media = CkJsonObjectW_IntOf(json,L"featured_media");
        comment_status = CkJsonObjectW_stringOf(json,L"comment_status");
        ping_status = CkJsonObjectW_stringOf(json,L"ping_status");
        sticky = CkJsonObjectW_BoolOf(json,L"sticky");
        template = CkJsonObjectW_stringOf(json,L"template");
        format = CkJsonObjectW_stringOf(json,L"format");
        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(json,L"meta");
        while (j < count_j) {
            CkJsonObjectW_putJ(json,j);
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(json,L"categories");
        while (j < count_j) {
            CkJsonObjectW_putJ(json,j);
            intVal = CkJsonObjectW_IntOf(json,L"categories[j]");
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(json,L"tags");
        while (j < count_j) {
            CkJsonObjectW_putJ(json,j);
            intVal = CkJsonObjectW_IntOf(json,L"tags[j]");
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(json,L"_links.self");
        while (j < count_j) {
            CkJsonObjectW_putJ(json,j);
            href = CkJsonObjectW_stringOf(json,L"_links.self[j].href");
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(json,L"_links.collection");
        while (j < count_j) {
            CkJsonObjectW_putJ(json,j);
            href = CkJsonObjectW_stringOf(json,L"_links.collection[j].href");
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(json,L"_links.about");
        while (j < count_j) {
            CkJsonObjectW_putJ(json,j);
            href = CkJsonObjectW_stringOf(json,L"_links.about[j].href");
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(json,L"_links.author");
        while (j < count_j) {
            CkJsonObjectW_putJ(json,j);
            embeddable = CkJsonObjectW_BoolOf(json,L"_links.author[j].embeddable");
            href = CkJsonObjectW_stringOf(json,L"_links.author[j].href");
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(json,L"_links.replies");
        while (j < count_j) {
            CkJsonObjectW_putJ(json,j);
            embeddable = CkJsonObjectW_BoolOf(json,L"_links.replies[j].embeddable");
            href = CkJsonObjectW_stringOf(json,L"_links.replies[j].href");
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(json,L"_links.version-history");
        while (j < count_j) {
            CkJsonObjectW_putJ(json,j);
            count = CkJsonObjectW_IntOf(json,L"_links.version-history[j].count");
            href = CkJsonObjectW_stringOf(json,L"_links.version-history[j].href");
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(json,L"_links.predecessor-version");
        while (j < count_j) {
            CkJsonObjectW_putJ(json,j);
            id = CkJsonObjectW_IntOf(json,L"_links.predecessor-version[j].id");
            href = CkJsonObjectW_stringOf(json,L"_links.predecessor-version[j].href");
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(json,L"_links.wp:attachment");
        while (j < count_j) {
            CkJsonObjectW_putJ(json,j);
            href = CkJsonObjectW_stringOf(json,L"_links.wp:attachment[j].href");
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(json,L"_links.wp:term");
        while (j < count_j) {
            CkJsonObjectW_putJ(json,j);
            taxonomy = CkJsonObjectW_stringOf(json,L"_links.wp:term[j].taxonomy");
            embeddable = CkJsonObjectW_BoolOf(json,L"_links.wp:term[j].embeddable");
            href = CkJsonObjectW_stringOf(json,L"_links.wp:term[j].href");
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(json,L"_links.curies");
        while (j < count_j) {
            CkJsonObjectW_putJ(json,j);
            name = CkJsonObjectW_stringOf(json,L"_links.curies[j].name");
            href = CkJsonObjectW_stringOf(json,L"_links.curies[j].href");
            templated = CkJsonObjectW_BoolOf(json,L"_links.curies[j].templated");
            j = j + 1;
        }

        CkJsonObjectW_Dispose(json);
        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonArrayW_Dispose(jarrResp);
    CkDtObjW_Dispose(date_gmt);

    }