Dart
Dart
WordPress Basic Authentication with miniOrange Plugin
See more WordPress Examples
Demonstrates basic username/password authentication using the miniOrange API Authentication plugin.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
final http = CkHttp();
// Implements the following CURL command:
// curl -X GET --user wp_username:wp_password https://www.yoursite.com/wp-json/wp/v2/posts?page=1
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
http.login = 'wp_username';
http.password = 'wp_password';
http.basicAuth = true;
final sbResponseBody = CkStringBuilder();
try {
http.quickGetSb('https://www.yoursite.com/wp-json/wp/v2/posts?page=1', sbResponseBody);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final jarrResp = CkJsonArray();
jarrResp.loadSb(sbResponseBody);
jarrResp.emitCompact = false;
print('Response Body:');
print(jarrResp.emit());
final respStatusCode = http.lastStatus;
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
final dateGmt = CkDtObj();
var id = 0;
var date = '';
var guidRendered = '';
var modified = '';
var modifiedGmt = '';
var slug = '';
var status = '';
var vType = '';
var link = '';
var titleRendered = '';
var contentRendered = '';
var excerptRendered = '';
var author = 0;
var featuredMedia = 0;
var commentStatus = '';
var pingStatus = '';
var template = '';
var format = '';
var j = 0;
var countJ = 0;
var intVal = 0;
var href = '';
var count = 0;
var taxonomy = '';
var name = '';
var i = 0;
final countI = jarrResp.size;
while (i < countI) {
final json = jarrResp.objectAt(i);
id = json.intOf('id');
date = json.stringOf('date');
json.dtOf('date_gmt', false, dateGmt);
guidRendered = json.stringOf('guid.rendered');
modified = json.stringOf('modified');
modifiedGmt = json.stringOf('modified_gmt');
slug = json.stringOf('slug');
status = json.stringOf('status');
vType = json.stringOf('type');
link = json.stringOf('link');
titleRendered = json.stringOf('title.rendered');
contentRendered = json.stringOf('content.rendered');
json.boolOf('content.protected');
excerptRendered = json.stringOf('excerpt.rendered');
json.boolOf('excerpt.protected');
author = json.intOf('author');
featuredMedia = json.intOf('featured_media');
commentStatus = json.stringOf('comment_status');
pingStatus = json.stringOf('ping_status');
json.boolOf('sticky');
template = json.stringOf('template');
format = json.stringOf('format');
j = 0;
countJ = json.sizeOfArray('meta');
while (j < countJ) {
json.j = j;
j++;
}
j = 0;
countJ = json.sizeOfArray('categories');
while (j < countJ) {
json.j = j;
intVal = json.intOf('categories[j]');
j++;
}
j = 0;
countJ = json.sizeOfArray('tags');
while (j < countJ) {
json.j = j;
intVal = json.intOf('tags[j]');
j++;
}
j = 0;
countJ = json.sizeOfArray('_links.self');
while (j < countJ) {
json.j = j;
href = json.stringOf('_links.self[j].href');
j++;
}
j = 0;
countJ = json.sizeOfArray('_links.collection');
while (j < countJ) {
json.j = j;
href = json.stringOf('_links.collection[j].href');
j++;
}
j = 0;
countJ = json.sizeOfArray('_links.about');
while (j < countJ) {
json.j = j;
href = json.stringOf('_links.about[j].href');
j++;
}
j = 0;
countJ = json.sizeOfArray('_links.author');
while (j < countJ) {
json.j = j;
json.boolOf('_links.author[j].embeddable');
href = json.stringOf('_links.author[j].href');
j++;
}
j = 0;
countJ = json.sizeOfArray('_links.replies');
while (j < countJ) {
json.j = j;
json.boolOf('_links.replies[j].embeddable');
href = json.stringOf('_links.replies[j].href');
j++;
}
j = 0;
countJ = json.sizeOfArray('_links.version-history');
while (j < countJ) {
json.j = j;
count = json.intOf('_links.version-history[j].count');
href = json.stringOf('_links.version-history[j].href');
j++;
}
j = 0;
countJ = json.sizeOfArray('_links.predecessor-version');
while (j < countJ) {
json.j = j;
id = json.intOf('_links.predecessor-version[j].id');
href = json.stringOf('_links.predecessor-version[j].href');
j++;
}
j = 0;
countJ = json.sizeOfArray('_links.wp:attachment');
while (j < countJ) {
json.j = j;
href = json.stringOf('_links.wp:attachment[j].href');
j++;
}
j = 0;
countJ = json.sizeOfArray('_links.wp:term');
while (j < countJ) {
json.j = j;
taxonomy = json.stringOf('_links.wp:term[j].taxonomy');
json.boolOf('_links.wp:term[j].embeddable');
href = json.stringOf('_links.wp:term[j].href');
j++;
}
j = 0;
countJ = json.sizeOfArray('_links.curies');
while (j < countJ) {
json.j = j;
name = json.stringOf('_links.curies[j].name');
href = json.stringOf('_links.curies[j].href');
json.boolOf('_links.curies[j].templated');
j++;
}
i++;
}
}