Rust
Rust
WordPress Authentication with Applications Password Plugin
See more WordPress Examples
Demonstrates basic username/password authentication using the WordPress Applications Password plugin.Chilkat Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = chilkat::Http::new();
// Implements the following CURL command:
// curl -X GET --user wp_username:app_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
// Use your WordPress login, such as "admin", not the application name.
http.set_login("wp_username");
// Use the application password, such as "Nths RwVH eDJ4 weNZ orMN jabq"
http.set_password("app_password");
http.set_basic_auth(true);
let sb_response_body = chilkat::StringBuilder::new();
if http.quick_get_sb("https://www.yoursite.com/wp-json/wp/v2/posts?page=1", &sb_response_body).is_err() {
println!("{}", http.last_error_text());
return;
}
let jarr_resp = chilkat::JsonArray::new();
let _ = jarr_resp.load_sb(&sb_response_body);
jarr_resp.set_emit_compact(false);
println!("Response Body:");
println!("{}", jarr_resp.emit().unwrap_or_default());
let resp_status_code = http.last_status();
println!("Response Status Code = {}", resp_status_code);
if resp_status_code >= 400 {
println!("Response Header:");
println!("{}", http.last_header());
println!("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 = chilkat::DtObj::new();
let mut j: i32 = 0;
let mut count_j: i32 = 0;
let mut i = 0;
let count_i = jarr_resp.size();
while i < count_i {
let json = jarr_resp.object_at(i).unwrap();
let _ = json.int_of("id");
let _ = json.string_of("date").unwrap_or_default();
let _ = json.dt_of("date_gmt", false, &date_gmt);
let _ = json.string_of("guid.rendered").unwrap_or_default();
let _ = json.string_of("modified").unwrap_or_default();
let _ = json.string_of("modified_gmt").unwrap_or_default();
let _ = json.string_of("slug").unwrap_or_default();
let _ = json.string_of("status").unwrap_or_default();
let _ = json.string_of("type").unwrap_or_default();
let _ = json.string_of("link").unwrap_or_default();
let _ = json.string_of("title.rendered").unwrap_or_default();
let _ = json.string_of("content.rendered").unwrap_or_default();
let _ = json.bool_of("content.protected");
let _ = json.string_of("excerpt.rendered").unwrap_or_default();
let _ = json.bool_of("excerpt.protected");
let _ = json.int_of("author");
let _ = json.int_of("featured_media");
let _ = json.string_of("comment_status").unwrap_or_default();
let _ = json.string_of("ping_status").unwrap_or_default();
let _ = json.bool_of("sticky");
let _ = json.string_of("template").unwrap_or_default();
let _ = json.string_of("format").unwrap_or_default();
j = 0;
count_j = json.size_of_array("meta");
while j < count_j {
json.set_j(j);
j = j + 1;
}
j = 0;
count_j = json.size_of_array("categories");
while j < count_j {
json.set_j(j);
let _ = json.int_of("categories[j]");
j = j + 1;
}
j = 0;
count_j = json.size_of_array("tags");
while j < count_j {
json.set_j(j);
let _ = json.int_of("tags[j]");
j = j + 1;
}
j = 0;
count_j = json.size_of_array("_links.self");
while j < count_j {
json.set_j(j);
let _ = json.string_of("_links.self[j].href").unwrap_or_default();
j = j + 1;
}
j = 0;
count_j = json.size_of_array("_links.collection");
while j < count_j {
json.set_j(j);
let _ = json.string_of("_links.collection[j].href").unwrap_or_default();
j = j + 1;
}
j = 0;
count_j = json.size_of_array("_links.about");
while j < count_j {
json.set_j(j);
let _ = json.string_of("_links.about[j].href").unwrap_or_default();
j = j + 1;
}
j = 0;
count_j = json.size_of_array("_links.author");
while j < count_j {
json.set_j(j);
let _ = json.bool_of("_links.author[j].embeddable");
let _ = json.string_of("_links.author[j].href").unwrap_or_default();
j = j + 1;
}
j = 0;
count_j = json.size_of_array("_links.replies");
while j < count_j {
json.set_j(j);
let _ = json.bool_of("_links.replies[j].embeddable");
let _ = json.string_of("_links.replies[j].href").unwrap_or_default();
j = j + 1;
}
j = 0;
count_j = json.size_of_array("_links.version-history");
while j < count_j {
json.set_j(j);
let _ = json.int_of("_links.version-history[j].count");
let _ = json.string_of("_links.version-history[j].href").unwrap_or_default();
j = j + 1;
}
j = 0;
count_j = json.size_of_array("_links.predecessor-version");
while j < count_j {
json.set_j(j);
let _ = json.int_of("_links.predecessor-version[j].id");
let _ = json.string_of("_links.predecessor-version[j].href").unwrap_or_default();
j = j + 1;
}
j = 0;
count_j = json.size_of_array("_links.wp:attachment");
while j < count_j {
json.set_j(j);
let _ = json.string_of("_links.wp:attachment[j].href").unwrap_or_default();
j = j + 1;
}
j = 0;
count_j = json.size_of_array("_links.wp:term");
while j < count_j {
json.set_j(j);
let _ = json.string_of("_links.wp:term[j].taxonomy").unwrap_or_default();
let _ = json.bool_of("_links.wp:term[j].embeddable");
let _ = json.string_of("_links.wp:term[j].href").unwrap_or_default();
j = j + 1;
}
j = 0;
count_j = json.size_of_array("_links.curies");
while j < count_j {
json.set_j(j);
let _ = json.string_of("_links.curies[j].name").unwrap_or_default();
let _ = json.string_of("_links.curies[j].href").unwrap_or_default();
let _ = json.bool_of("_links.curies[j].templated");
j = j + 1;
}
i = i + 1;
}