Sample code for 30+ languages & platforms
PureBasic

WordPress API Key Authentication with miniOrange Plugin

See more WordPress Examples

Demonstrates API key authentication using the miniOrange API Authentication plugin.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonArray.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkDtObj.pb"

Procedure ChilkatExample()

    success.i = 0

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckQuickGetSb(http,"https://www.yoursite.com/wp-json/wp/v2/posts?page=1",sbResponseBody)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    jarrResp.i = CkJsonArray::ckCreate()
    If jarrResp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonArray::ckLoadSb(jarrResp,sbResponseBody)
    CkJsonArray::setCkEmitCompact(jarrResp, 0)

    Debug "Response Body:"
    Debug CkJsonArray::ckEmit(jarrResp)

    respStatusCode.i = CkHttp::ckLastStatus(http)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttp::ckLastHeader(http)
        Debug "Failed."
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonArray::ckDispose(jarrResp)
        ProcedureReturn
    EndIf

    ; 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.i = CkDtObj::ckCreate()
    If date_gmt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    json.i
    id.i
    date.s
    guidRendered.s
    modified.s
    modified_gmt.s
    slug.s
    status.s
    v_type.s
    link.s
    titleRendered.s
    contentRendered.s
    contentProtected.i
    excerptRendered.s
    excerptProtected.i
    author.i
    featured_media.i
    comment_status.s
    ping_status.s
    sticky.i
    template.s
    format.s
    j.i
    count_j.i
    intVal.i
    href.s
    embeddable.i
    count.i
    taxonomy.s
    name.s
    templated.i

    i.i = 0
    count_i.i = CkJsonArray::ckSize(jarrResp)
    While i < count_i
        json = CkJsonArray::ckObjectAt(jarrResp,i)
        id = CkJsonObject::ckIntOf(json,"id")
        date = CkJsonObject::ckStringOf(json,"date")
        CkJsonObject::ckDtOf(json,"date_gmt",0,date_gmt)
        guidRendered = CkJsonObject::ckStringOf(json,"guid.rendered")
        modified = CkJsonObject::ckStringOf(json,"modified")
        modified_gmt = CkJsonObject::ckStringOf(json,"modified_gmt")
        slug = CkJsonObject::ckStringOf(json,"slug")
        status = CkJsonObject::ckStringOf(json,"status")
        v_type = CkJsonObject::ckStringOf(json,"type")
        link = CkJsonObject::ckStringOf(json,"link")
        titleRendered = CkJsonObject::ckStringOf(json,"title.rendered")
        contentRendered = CkJsonObject::ckStringOf(json,"content.rendered")
        contentProtected = CkJsonObject::ckBoolOf(json,"content.protected")
        excerptRendered = CkJsonObject::ckStringOf(json,"excerpt.rendered")
        excerptProtected = CkJsonObject::ckBoolOf(json,"excerpt.protected")
        author = CkJsonObject::ckIntOf(json,"author")
        featured_media = CkJsonObject::ckIntOf(json,"featured_media")
        comment_status = CkJsonObject::ckStringOf(json,"comment_status")
        ping_status = CkJsonObject::ckStringOf(json,"ping_status")
        sticky = CkJsonObject::ckBoolOf(json,"sticky")
        template = CkJsonObject::ckStringOf(json,"template")
        format = CkJsonObject::ckStringOf(json,"format")
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"meta")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            j = j + 1
        Wend
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"categories")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            intVal = CkJsonObject::ckIntOf(json,"categories[j]")
            j = j + 1
        Wend
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"tags")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            intVal = CkJsonObject::ckIntOf(json,"tags[j]")
            j = j + 1
        Wend
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"_links.self")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            href = CkJsonObject::ckStringOf(json,"_links.self[j].href")
            j = j + 1
        Wend
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"_links.collection")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            href = CkJsonObject::ckStringOf(json,"_links.collection[j].href")
            j = j + 1
        Wend
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"_links.about")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            href = CkJsonObject::ckStringOf(json,"_links.about[j].href")
            j = j + 1
        Wend
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"_links.author")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            embeddable = CkJsonObject::ckBoolOf(json,"_links.author[j].embeddable")
            href = CkJsonObject::ckStringOf(json,"_links.author[j].href")
            j = j + 1
        Wend
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"_links.replies")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            embeddable = CkJsonObject::ckBoolOf(json,"_links.replies[j].embeddable")
            href = CkJsonObject::ckStringOf(json,"_links.replies[j].href")
            j = j + 1
        Wend
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"_links.version-history")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            count = CkJsonObject::ckIntOf(json,"_links.version-history[j].count")
            href = CkJsonObject::ckStringOf(json,"_links.version-history[j].href")
            j = j + 1
        Wend
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"_links.predecessor-version")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            id = CkJsonObject::ckIntOf(json,"_links.predecessor-version[j].id")
            href = CkJsonObject::ckStringOf(json,"_links.predecessor-version[j].href")
            j = j + 1
        Wend
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"_links.wp:attachment")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            href = CkJsonObject::ckStringOf(json,"_links.wp:attachment[j].href")
            j = j + 1
        Wend
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"_links.wp:term")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            taxonomy = CkJsonObject::ckStringOf(json,"_links.wp:term[j].taxonomy")
            embeddable = CkJsonObject::ckBoolOf(json,"_links.wp:term[j].embeddable")
            href = CkJsonObject::ckStringOf(json,"_links.wp:term[j].href")
            j = j + 1
        Wend
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"_links.curies")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            name = CkJsonObject::ckStringOf(json,"_links.curies[j].name")
            href = CkJsonObject::ckStringOf(json,"_links.curies[j].href")
            templated = CkJsonObject::ckBoolOf(json,"_links.curies[j].templated")
            j = j + 1
        Wend
        CkJsonObject::ckDispose(json)

        i = i + 1
    Wend


    CkHttp::ckDispose(http)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonArray::ckDispose(jarrResp)
    CkDtObj::ckDispose(date_gmt)


    ProcedureReturn
EndProcedure