Lua
Lua
WordPress API Key Authentication with miniOrange Plugin
See more WordPress Examples
Demonstrates API key authentication using the miniOrange API Authentication plugin.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- Use your API key here, such as TaVFjSBu8IMR0MbvZNn7A6P04GXrbtHm
-- This causes the "Authorization: Bearer <api_key>" header to be added to each HTTP request.
http.auth_token = "api_key"
local sbResponseBody = chilkat.StringBuilder.new()
local success = http:quick_get_sb("https://www.yoursite.com/wp-json/wp/v2/posts?page=1", sbResponseBody)
if not success then
print(http.last_error_text)
return
end
local jarrResp = chilkat.JsonArray.new()
jarrResp:load_sb(sbResponseBody)
jarrResp.emit_compact = false
print("Response Body:")
print(jarrResp:emit())
local respStatusCode = http.last_status
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
print("Response Header:")
print(http.last_header)
print("Failed.")
return
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
local date_gmt = chilkat.DtObj.new()
local json
local id = 0
local date = ""
local guidRendered = ""
local modified = ""
local modified_gmt = ""
local slug = ""
local status = ""
local v_type = ""
local link = ""
local titleRendered = ""
local contentRendered = ""
local contentProtected = false
local excerptRendered = ""
local excerptProtected = false
local author = 0
local featured_media = 0
local comment_status = ""
local ping_status = ""
local sticky = false
local template = ""
local format = ""
local j = 0
local count_j = 0
local intVal = 0
local href = ""
local embeddable = false
local count = 0
local taxonomy = ""
local name = ""
local templated = false
local i = 0
local count_i = jarrResp.size
while i < count_i do
json = jarrResp:object_at(i)
id = json:int_of("id")
date = json:string_of("date")
json:dt_of("date_gmt", false, date_gmt)
guidRendered = json:string_of("guid.rendered")
modified = json:string_of("modified")
modified_gmt = json:string_of("modified_gmt")
slug = json:string_of("slug")
status = json:string_of("status")
v_type = json:string_of("type")
link = json:string_of("link")
titleRendered = json:string_of("title.rendered")
contentRendered = json:string_of("content.rendered")
contentProtected = json:bool_of("content.protected")
excerptRendered = json:string_of("excerpt.rendered")
excerptProtected = json:bool_of("excerpt.protected")
author = json:int_of("author")
featured_media = json:int_of("featured_media")
comment_status = json:string_of("comment_status")
ping_status = json:string_of("ping_status")
sticky = json:bool_of("sticky")
template = json:string_of("template")
format = json:string_of("format")
j = 0
count_j = json:size_of_array("meta")
while j < count_j do
json.j = j
j = j + 1
end
j = 0
count_j = json:size_of_array("categories")
while j < count_j do
json.j = j
intVal = json:int_of("categories[j]")
j = j + 1
end
j = 0
count_j = json:size_of_array("tags")
while j < count_j do
json.j = j
intVal = json:int_of("tags[j]")
j = j + 1
end
j = 0
count_j = json:size_of_array("_links.self")
while j < count_j do
json.j = j
href = json:string_of("_links.self[j].href")
j = j + 1
end
j = 0
count_j = json:size_of_array("_links.collection")
while j < count_j do
json.j = j
href = json:string_of("_links.collection[j].href")
j = j + 1
end
j = 0
count_j = json:size_of_array("_links.about")
while j < count_j do
json.j = j
href = json:string_of("_links.about[j].href")
j = j + 1
end
j = 0
count_j = json:size_of_array("_links.author")
while j < count_j do
json.j = j
embeddable = json:bool_of("_links.author[j].embeddable")
href = json:string_of("_links.author[j].href")
j = j + 1
end
j = 0
count_j = json:size_of_array("_links.replies")
while j < count_j do
json.j = j
embeddable = json:bool_of("_links.replies[j].embeddable")
href = json:string_of("_links.replies[j].href")
j = j + 1
end
j = 0
count_j = json:size_of_array("_links.version-history")
while j < count_j do
json.j = j
count = json:int_of("_links.version-history[j].count")
href = json:string_of("_links.version-history[j].href")
j = j + 1
end
j = 0
count_j = json:size_of_array("_links.predecessor-version")
while j < count_j do
json.j = j
id = json:int_of("_links.predecessor-version[j].id")
href = json:string_of("_links.predecessor-version[j].href")
j = j + 1
end
j = 0
count_j = json:size_of_array("_links.wp:attachment")
while j < count_j do
json.j = j
href = json:string_of("_links.wp:attachment[j].href")
j = j + 1
end
j = 0
count_j = json:size_of_array("_links.wp:term")
while j < count_j do
json.j = j
taxonomy = json:string_of("_links.wp:term[j].taxonomy")
embeddable = json:bool_of("_links.wp:term[j].embeddable")
href = json:string_of("_links.wp:term[j].href")
j = j + 1
end
j = 0
count_j = json:size_of_array("_links.curies")
while j < count_j do
json.j = j
name = json:string_of("_links.curies[j].name")
href = json:string_of("_links.curies[j].href")
templated = json:bool_of("_links.curies[j].templated")
j = j + 1
end
i = i + 1
end