Delphi DLL
Delphi DLL
WordPress API Key Authentication with miniOrange Plugin
See more WordPress Examples
Demonstrates API key authentication using the miniOrange API Authentication plugin.Chilkat Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, StringBuilder, DtObj, JsonArray, JsonObject;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
http: HCkHttp;
sbResponseBody: HCkStringBuilder;
jarrResp: HCkJsonArray;
respStatusCode: Integer;
date_gmt: HCkDtObj;
json: HCkJsonObject;
id: Integer;
date: PWideChar;
guidRendered: PWideChar;
modified: PWideChar;
modified_gmt: PWideChar;
slug: PWideChar;
status: PWideChar;
v_type: PWideChar;
link: PWideChar;
titleRendered: PWideChar;
contentRendered: PWideChar;
contentProtected: Boolean;
excerptRendered: PWideChar;
excerptProtected: Boolean;
author: Integer;
featured_media: Integer;
comment_status: PWideChar;
ping_status: PWideChar;
sticky: Boolean;
template: PWideChar;
format: PWideChar;
j: Integer;
count_j: Integer;
intVal: Integer;
href: PWideChar;
embeddable: Boolean;
count: Integer;
taxonomy: PWideChar;
name: PWideChar;
templated: Boolean;
i: Integer;
count_i: Integer;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := CkHttp_Create();
// Use your API key here, such as TaVFjSBu8IMR0MbvZNn7A6P04GXrbtHm
// This causes the "Authorization: Bearer <api_key>" header to be added to each HTTP request.
CkHttp_putAuthToken(http,'api_key');
sbResponseBody := CkStringBuilder_Create();
success := CkHttp_QuickGetSb(http,'https://www.yoursite.com/wp-json/wp/v2/posts?page=1',sbResponseBody);
if (success = False) then
begin
Memo1.Lines.Add(CkHttp__lastErrorText(http));
Exit;
end;
jarrResp := CkJsonArray_Create();
CkJsonArray_LoadSb(jarrResp,sbResponseBody);
CkJsonArray_putEmitCompact(jarrResp,False);
Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(CkJsonArray__emit(jarrResp));
respStatusCode := CkHttp_getLastStatus(http);
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode >= 400) then
begin
Memo1.Lines.Add('Response Header:');
Memo1.Lines.Add(CkHttp__lastHeader(http));
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 := CkDtObj_Create();
i := 0;
count_i := CkJsonArray_getSize(jarrResp);
while i < count_i do
begin
json := CkJsonArray_ObjectAt(jarrResp,i);
id := CkJsonObject_IntOf(json,'id');
date := CkJsonObject__stringOf(json,'date');
CkJsonObject_DtOf(json,'date_gmt',False,date_gmt);
guidRendered := CkJsonObject__stringOf(json,'guid.rendered');
modified := CkJsonObject__stringOf(json,'modified');
modified_gmt := CkJsonObject__stringOf(json,'modified_gmt');
slug := CkJsonObject__stringOf(json,'slug');
status := CkJsonObject__stringOf(json,'status');
v_type := CkJsonObject__stringOf(json,'type');
link := CkJsonObject__stringOf(json,'link');
titleRendered := CkJsonObject__stringOf(json,'title.rendered');
contentRendered := CkJsonObject__stringOf(json,'content.rendered');
contentProtected := CkJsonObject_BoolOf(json,'content.protected');
excerptRendered := CkJsonObject__stringOf(json,'excerpt.rendered');
excerptProtected := CkJsonObject_BoolOf(json,'excerpt.protected');
author := CkJsonObject_IntOf(json,'author');
featured_media := CkJsonObject_IntOf(json,'featured_media');
comment_status := CkJsonObject__stringOf(json,'comment_status');
ping_status := CkJsonObject__stringOf(json,'ping_status');
sticky := CkJsonObject_BoolOf(json,'sticky');
template := CkJsonObject__stringOf(json,'template');
format := CkJsonObject__stringOf(json,'format');
j := 0;
count_j := CkJsonObject_SizeOfArray(json,'meta');
while j < count_j do
begin
CkJsonObject_putJ(json,j);
j := j + 1;
end;
j := 0;
count_j := CkJsonObject_SizeOfArray(json,'categories');
while j < count_j do
begin
CkJsonObject_putJ(json,j);
intVal := CkJsonObject_IntOf(json,'categories[j]');
j := j + 1;
end;
j := 0;
count_j := CkJsonObject_SizeOfArray(json,'tags');
while j < count_j do
begin
CkJsonObject_putJ(json,j);
intVal := CkJsonObject_IntOf(json,'tags[j]');
j := j + 1;
end;
j := 0;
count_j := CkJsonObject_SizeOfArray(json,'_links.self');
while j < count_j do
begin
CkJsonObject_putJ(json,j);
href := CkJsonObject__stringOf(json,'_links.self[j].href');
j := j + 1;
end;
j := 0;
count_j := CkJsonObject_SizeOfArray(json,'_links.collection');
while j < count_j do
begin
CkJsonObject_putJ(json,j);
href := CkJsonObject__stringOf(json,'_links.collection[j].href');
j := j + 1;
end;
j := 0;
count_j := CkJsonObject_SizeOfArray(json,'_links.about');
while j < count_j do
begin
CkJsonObject_putJ(json,j);
href := CkJsonObject__stringOf(json,'_links.about[j].href');
j := j + 1;
end;
j := 0;
count_j := CkJsonObject_SizeOfArray(json,'_links.author');
while j < count_j do
begin
CkJsonObject_putJ(json,j);
embeddable := CkJsonObject_BoolOf(json,'_links.author[j].embeddable');
href := CkJsonObject__stringOf(json,'_links.author[j].href');
j := j + 1;
end;
j := 0;
count_j := CkJsonObject_SizeOfArray(json,'_links.replies');
while j < count_j do
begin
CkJsonObject_putJ(json,j);
embeddable := CkJsonObject_BoolOf(json,'_links.replies[j].embeddable');
href := CkJsonObject__stringOf(json,'_links.replies[j].href');
j := j + 1;
end;
j := 0;
count_j := CkJsonObject_SizeOfArray(json,'_links.version-history');
while j < count_j do
begin
CkJsonObject_putJ(json,j);
count := CkJsonObject_IntOf(json,'_links.version-history[j].count');
href := CkJsonObject__stringOf(json,'_links.version-history[j].href');
j := j + 1;
end;
j := 0;
count_j := CkJsonObject_SizeOfArray(json,'_links.predecessor-version');
while j < count_j do
begin
CkJsonObject_putJ(json,j);
id := CkJsonObject_IntOf(json,'_links.predecessor-version[j].id');
href := CkJsonObject__stringOf(json,'_links.predecessor-version[j].href');
j := j + 1;
end;
j := 0;
count_j := CkJsonObject_SizeOfArray(json,'_links.wp:attachment');
while j < count_j do
begin
CkJsonObject_putJ(json,j);
href := CkJsonObject__stringOf(json,'_links.wp:attachment[j].href');
j := j + 1;
end;
j := 0;
count_j := CkJsonObject_SizeOfArray(json,'_links.wp:term');
while j < count_j do
begin
CkJsonObject_putJ(json,j);
taxonomy := CkJsonObject__stringOf(json,'_links.wp:term[j].taxonomy');
embeddable := CkJsonObject_BoolOf(json,'_links.wp:term[j].embeddable');
href := CkJsonObject__stringOf(json,'_links.wp:term[j].href');
j := j + 1;
end;
j := 0;
count_j := CkJsonObject_SizeOfArray(json,'_links.curies');
while j < count_j do
begin
CkJsonObject_putJ(json,j);
name := CkJsonObject__stringOf(json,'_links.curies[j].name');
href := CkJsonObject__stringOf(json,'_links.curies[j].href');
templated := CkJsonObject_BoolOf(json,'_links.curies[j].templated');
j := j + 1;
end;
CkJsonObject_Dispose(json);
i := i + 1;
end;
CkHttp_Dispose(http);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonArray_Dispose(jarrResp);
CkDtObj_Dispose(date_gmt);
end;