Sample code for 30+ languages & platforms
Tcl

WordPress API Key Authentication with miniOrange Plugin

See more WordPress Examples

Demonstrates API key authentication using the miniOrange API Authentication plugin.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

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

set http [new_CkHttp]

# Use your API key here, such as TaVFjSBu8IMR0MbvZNn7A6P04GXrbtHm
# This causes the "Authorization: Bearer <api_key>" header to be added to each HTTP request.
CkHttp_put_AuthToken $http "api_key"

set sbResponseBody [new_CkStringBuilder]

set success [CkHttp_QuickGetSb $http "https://www.yoursite.com/wp-json/wp/v2/posts?page=1" $sbResponseBody]
if {$success == 0} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    delete_CkStringBuilder $sbResponseBody
    exit
}

set jarrResp [new_CkJsonArray]

CkJsonArray_LoadSb $jarrResp $sbResponseBody
CkJsonArray_put_EmitCompact $jarrResp 0

puts "Response Body:"
puts [CkJsonArray_emit $jarrResp]

set respStatusCode [CkHttp_get_LastStatus $http]
puts "Response Status Code = $respStatusCode"
if {$respStatusCode >= 400} then {
    puts "Response Header:"
    puts [CkHttp_lastHeader $http]
    puts "Failed."
    delete_CkHttp $http
    delete_CkStringBuilder $sbResponseBody
    delete_CkJsonArray $jarrResp
    exit
}

# 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

set date_gmt [new_CkDtObj]

# json is a CkJsonObject

set i 0
set count_i [CkJsonArray_get_Size $jarrResp]
while {$i < $count_i} {
    set json [CkJsonArray_ObjectAt $jarrResp $i]
    set id [CkJsonObject_IntOf $json "id"]
    set date [CkJsonObject_stringOf $json "date"]
    CkJsonObject_DtOf $json "date_gmt" 0 $date_gmt
    set guidRendered [CkJsonObject_stringOf $json "guid.rendered"]
    set modified [CkJsonObject_stringOf $json "modified"]
    set modified_gmt [CkJsonObject_stringOf $json "modified_gmt"]
    set slug [CkJsonObject_stringOf $json "slug"]
    set status [CkJsonObject_stringOf $json "status"]
    set v_type [CkJsonObject_stringOf $json "type"]
    set link [CkJsonObject_stringOf $json "link"]
    set titleRendered [CkJsonObject_stringOf $json "title.rendered"]
    set contentRendered [CkJsonObject_stringOf $json "content.rendered"]
    set contentProtected [CkJsonObject_BoolOf $json "content.protected"]
    set excerptRendered [CkJsonObject_stringOf $json "excerpt.rendered"]
    set excerptProtected [CkJsonObject_BoolOf $json "excerpt.protected"]
    set author [CkJsonObject_IntOf $json "author"]
    set featured_media [CkJsonObject_IntOf $json "featured_media"]
    set comment_status [CkJsonObject_stringOf $json "comment_status"]
    set ping_status [CkJsonObject_stringOf $json "ping_status"]
    set sticky [CkJsonObject_BoolOf $json "sticky"]
    set template [CkJsonObject_stringOf $json "template"]
    set format [CkJsonObject_stringOf $json "format"]
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "meta"]
    while {$j < $count_j} {
        CkJsonObject_put_J $json $j
        set j [expr $j + 1]
    }
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "categories"]
    while {$j < $count_j} {
        CkJsonObject_put_J $json $j
        set intVal [CkJsonObject_IntOf $json "categories[j]"]
        set j [expr $j + 1]
    }
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "tags"]
    while {$j < $count_j} {
        CkJsonObject_put_J $json $j
        set intVal [CkJsonObject_IntOf $json "tags[j]"]
        set j [expr $j + 1]
    }
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "_links.self"]
    while {$j < $count_j} {
        CkJsonObject_put_J $json $j
        set href [CkJsonObject_stringOf $json "_links.self[j].href"]
        set j [expr $j + 1]
    }
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "_links.collection"]
    while {$j < $count_j} {
        CkJsonObject_put_J $json $j
        set href [CkJsonObject_stringOf $json "_links.collection[j].href"]
        set j [expr $j + 1]
    }
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "_links.about"]
    while {$j < $count_j} {
        CkJsonObject_put_J $json $j
        set href [CkJsonObject_stringOf $json "_links.about[j].href"]
        set j [expr $j + 1]
    }
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "_links.author"]
    while {$j < $count_j} {
        CkJsonObject_put_J $json $j
        set embeddable [CkJsonObject_BoolOf $json "_links.author[j].embeddable"]
        set href [CkJsonObject_stringOf $json "_links.author[j].href"]
        set j [expr $j + 1]
    }
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "_links.replies"]
    while {$j < $count_j} {
        CkJsonObject_put_J $json $j
        set embeddable [CkJsonObject_BoolOf $json "_links.replies[j].embeddable"]
        set href [CkJsonObject_stringOf $json "_links.replies[j].href"]
        set j [expr $j + 1]
    }
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "_links.version-history"]
    while {$j < $count_j} {
        CkJsonObject_put_J $json $j
        set count [CkJsonObject_IntOf $json "_links.version-history[j].count"]
        set href [CkJsonObject_stringOf $json "_links.version-history[j].href"]
        set j [expr $j + 1]
    }
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "_links.predecessor-version"]
    while {$j < $count_j} {
        CkJsonObject_put_J $json $j
        set id [CkJsonObject_IntOf $json "_links.predecessor-version[j].id"]
        set href [CkJsonObject_stringOf $json "_links.predecessor-version[j].href"]
        set j [expr $j + 1]
    }
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "_links.wp:attachment"]
    while {$j < $count_j} {
        CkJsonObject_put_J $json $j
        set href [CkJsonObject_stringOf $json "_links.wp:attachment[j].href"]
        set j [expr $j + 1]
    }
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "_links.wp:term"]
    while {$j < $count_j} {
        CkJsonObject_put_J $json $j
        set taxonomy [CkJsonObject_stringOf $json "_links.wp:term[j].taxonomy"]
        set embeddable [CkJsonObject_BoolOf $json "_links.wp:term[j].embeddable"]
        set href [CkJsonObject_stringOf $json "_links.wp:term[j].href"]
        set j [expr $j + 1]
    }
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "_links.curies"]
    while {$j < $count_j} {
        CkJsonObject_put_J $json $j
        set name [CkJsonObject_stringOf $json "_links.curies[j].name"]
        set href [CkJsonObject_stringOf $json "_links.curies[j].href"]
        set templated [CkJsonObject_BoolOf $json "_links.curies[j].templated"]
        set j [expr $j + 1]
    }
    delete_CkJsonObject $json

    set i [expr $i + 1]
}

delete_CkHttp $http
delete_CkStringBuilder $sbResponseBody
delete_CkJsonArray $jarrResp
delete_CkDtObj $date_gmt