Swift
Swift
WordPress API Key Authentication with miniOrange Plugin
See more WordPress Examples
Demonstrates API key authentication using the miniOrange API Authentication plugin.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = CkoHttp()!
// 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"
let sbResponseBody = CkoStringBuilder()!
success = http.quickGetSb(url: "https://www.yoursite.com/wp-json/wp/v2/posts?page=1", sbContent: sbResponseBody)
if success == false {
print("\(http.lastErrorText!)")
return
}
let jarrResp = CkoJsonArray()!
jarrResp.loadSb(sb: sbResponseBody)
jarrResp.emitCompact = false
print("Response Body:")
print("\(jarrResp.emit()!)")
var respStatusCode: Int = http.lastStatus.intValue
print("Response Status Code = \(respStatusCode)")
if respStatusCode >= 400 {
print("Response Header:")
print("\(http.lastHeader!)")
print("Failed.")
return
}
// 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
let date_gmt = CkoDtObj()!
var json: CkoJsonObject?
var id: Int
var date: String?
var guidRendered: String?
var modified: String?
var modified_gmt: String?
var slug: String?
var status: String?
var v_type: String?
var link: String?
var titleRendered: String?
var contentRendered: String?
var contentProtected: Bool
var excerptRendered: String?
var excerptProtected: Bool
var author: Int
var featured_media: Int
var comment_status: String?
var ping_status: String?
var sticky: Bool
var template: String?
var format: String?
var j: Int
var count_j: Int
var intVal: Int
var href: String?
var embeddable: Bool
var count: Int
var taxonomy: String?
var name: String?
var templated: Bool
var i: Int = 0
var count_i: Int = jarrResp.size.intValue
while i < count_i {
json = jarrResp.object(at: i)
id = json!.int(of: "id").intValue
date = json!.string(of: "date")
json!.dt(of: "date_gmt", bLocal: false, dt: 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").intValue
featured_media = json!.int(of: "featured_media").intValue
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(ofArray: "meta").intValue
while j < count_j {
json.j = j
j = j + 1
}
j = 0
count_j = json!.size(ofArray: "categories").intValue
while j < count_j {
json.j = j
intVal = json!.int(of: "categories[j]").intValue
j = j + 1
}
j = 0
count_j = json!.size(ofArray: "tags").intValue
while j < count_j {
json.j = j
intVal = json!.int(of: "tags[j]").intValue
j = j + 1
}
j = 0
count_j = json!.size(ofArray: "_links.self").intValue
while j < count_j {
json.j = j
href = json!.string(of: "_links.self[j].href")
j = j + 1
}
j = 0
count_j = json!.size(ofArray: "_links.collection").intValue
while j < count_j {
json.j = j
href = json!.string(of: "_links.collection[j].href")
j = j + 1
}
j = 0
count_j = json!.size(ofArray: "_links.about").intValue
while j < count_j {
json.j = j
href = json!.string(of: "_links.about[j].href")
j = j + 1
}
j = 0
count_j = json!.size(ofArray: "_links.author").intValue
while j < count_j {
json.j = j
embeddable = json!.bool(of: "_links.author[j].embeddable")
href = json!.string(of: "_links.author[j].href")
j = j + 1
}
j = 0
count_j = json!.size(ofArray: "_links.replies").intValue
while j < count_j {
json.j = j
embeddable = json!.bool(of: "_links.replies[j].embeddable")
href = json!.string(of: "_links.replies[j].href")
j = j + 1
}
j = 0
count_j = json!.size(ofArray: "_links.version-history").intValue
while j < count_j {
json.j = j
count = json!.int(of: "_links.version-history[j].count").intValue
href = json!.string(of: "_links.version-history[j].href")
j = j + 1
}
j = 0
count_j = json!.size(ofArray: "_links.predecessor-version").intValue
while j < count_j {
json.j = j
id = json!.int(of: "_links.predecessor-version[j].id").intValue
href = json!.string(of: "_links.predecessor-version[j].href")
j = j + 1
}
j = 0
count_j = json!.size(ofArray: "_links.wp:attachment").intValue
while j < count_j {
json.j = j
href = json!.string(of: "_links.wp:attachment[j].href")
j = j + 1
}
j = 0
count_j = json!.size(ofArray: "_links.wp:term").intValue
while j < count_j {
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
}
j = 0
count_j = json!.size(ofArray: "_links.curies").intValue
while j < count_j {
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
}
json = nil
i = i + 1
}
}