Sample code for 30+ languages & platforms
Delphi ActiveX

WordPress API Key Authentication with miniOrange Plugin

See more WordPress Examples

Demonstrates API key authentication using the miniOrange API Authentication plugin.

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
http: TChilkatHttp;
sbResponseBody: TChilkatStringBuilder;
jarrResp: TChilkatJsonArray;
respStatusCode: Integer;
date_gmt: TDtObj;
json: IChilkatJsonObject;
id: Integer;
date: WideString;
guidRendered: WideString;
modified: WideString;
modified_gmt: WideString;
slug: WideString;
status: WideString;
v_type: WideString;
link: WideString;
titleRendered: WideString;
contentRendered: WideString;
contentProtected: Integer;
excerptRendered: WideString;
excerptProtected: Integer;
author: Integer;
featured_media: Integer;
comment_status: WideString;
ping_status: WideString;
sticky: Integer;
template: WideString;
format: WideString;
j: Integer;
count_j: Integer;
intVal: Integer;
href: WideString;
embeddable: Integer;
count: Integer;
taxonomy: WideString;
name: WideString;
templated: Integer;
i: Integer;
count_i: Integer;

begin
success := 0;

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

http := TChilkatHttp.Create(Self);

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

sbResponseBody := TChilkatStringBuilder.Create(Self);
success := http.QuickGetSb('https://www.yoursite.com/wp-json/wp/v2/posts?page=1',sbResponseBody.ControlInterface);
if (success = 0) then
  begin
    Memo1.Lines.Add(http.LastErrorText);
    Exit;
  end;

jarrResp := TChilkatJsonArray.Create(Self);
jarrResp.LoadSb(sbResponseBody.ControlInterface);
jarrResp.EmitCompact := 0;

Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(jarrResp.Emit());

respStatusCode := http.LastStatus;
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(http.LastHeader);
    Memo1.Lines.Add('Failed.');
    Exit;
  end;

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

date_gmt := TDtObj.Create(Self);

i := 0;
count_i := jarrResp.Size;
while i < count_i do
  begin
    json := jarrResp.ObjectAt(i);
    id := json.IntOf('id');
    date := json.StringOf('date');
    json.DtOf('date_gmt',0,date_gmt.ControlInterface);
    guidRendered := json.StringOf('guid.rendered');
    modified := json.StringOf('modified');
    modified_gmt := json.StringOf('modified_gmt');
    slug := json.StringOf('slug');
    status := json.StringOf('status');
    v_type := json.StringOf('type');
    link := json.StringOf('link');
    titleRendered := json.StringOf('title.rendered');
    contentRendered := json.StringOf('content.rendered');
    contentProtected := json.BoolOf('content.protected');
    excerptRendered := json.StringOf('excerpt.rendered');
    excerptProtected := json.BoolOf('excerpt.protected');
    author := json.IntOf('author');
    featured_media := json.IntOf('featured_media');
    comment_status := json.StringOf('comment_status');
    ping_status := json.StringOf('ping_status');
    sticky := json.BoolOf('sticky');
    template := json.StringOf('template');
    format := json.StringOf('format');
    j := 0;
    count_j := json.SizeOfArray('meta');
    while j < count_j do
      begin
        json.J := j;
        j := j + 1;
      end;

    j := 0;
    count_j := json.SizeOfArray('categories');
    while j < count_j do
      begin
        json.J := j;
        intVal := json.IntOf('categories[j]');
        j := j + 1;
      end;

    j := 0;
    count_j := json.SizeOfArray('tags');
    while j < count_j do
      begin
        json.J := j;
        intVal := json.IntOf('tags[j]');
        j := j + 1;
      end;

    j := 0;
    count_j := json.SizeOfArray('_links.self');
    while j < count_j do
      begin
        json.J := j;
        href := json.StringOf('_links.self[j].href');
        j := j + 1;
      end;

    j := 0;
    count_j := json.SizeOfArray('_links.collection');
    while j < count_j do
      begin
        json.J := j;
        href := json.StringOf('_links.collection[j].href');
        j := j + 1;
      end;

    j := 0;
    count_j := json.SizeOfArray('_links.about');
    while j < count_j do
      begin
        json.J := j;
        href := json.StringOf('_links.about[j].href');
        j := j + 1;
      end;

    j := 0;
    count_j := json.SizeOfArray('_links.author');
    while j < count_j do
      begin
        json.J := j;
        embeddable := json.BoolOf('_links.author[j].embeddable');
        href := json.StringOf('_links.author[j].href');
        j := j + 1;
      end;

    j := 0;
    count_j := json.SizeOfArray('_links.replies');
    while j < count_j do
      begin
        json.J := j;
        embeddable := json.BoolOf('_links.replies[j].embeddable');
        href := json.StringOf('_links.replies[j].href');
        j := j + 1;
      end;

    j := 0;
    count_j := json.SizeOfArray('_links.version-history');
    while j < count_j do
      begin
        json.J := j;
        count := json.IntOf('_links.version-history[j].count');
        href := json.StringOf('_links.version-history[j].href');
        j := j + 1;
      end;

    j := 0;
    count_j := json.SizeOfArray('_links.predecessor-version');
    while j < count_j do
      begin
        json.J := j;
        id := json.IntOf('_links.predecessor-version[j].id');
        href := json.StringOf('_links.predecessor-version[j].href');
        j := j + 1;
      end;

    j := 0;
    count_j := json.SizeOfArray('_links.wp:attachment');
    while j < count_j do
      begin
        json.J := j;
        href := json.StringOf('_links.wp:attachment[j].href');
        j := j + 1;
      end;

    j := 0;
    count_j := json.SizeOfArray('_links.wp:term');
    while j < count_j do
      begin
        json.J := j;
        taxonomy := json.StringOf('_links.wp:term[j].taxonomy');
        embeddable := json.BoolOf('_links.wp:term[j].embeddable');
        href := json.StringOf('_links.wp:term[j].href');
        j := j + 1;
      end;

    j := 0;
    count_j := json.SizeOfArray('_links.curies');
    while j < count_j do
      begin
        json.J := j;
        name := json.StringOf('_links.curies[j].name');
        href := json.StringOf('_links.curies[j].href');
        templated := json.BoolOf('_links.curies[j].templated');
        j := j + 1;
      end;

    i := i + 1;
  end;
end;