Rust
Rust
Shopware 6 - Get OAuth2 Access Token using Username and Password
See more Shopware 6 Examples
Demonstrates how to get an OAuth2 access token using your username and password.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();
// Sends the following POST
// {
// "client_id": "administration",
// "grant_type": "password",
// "scopes": "write",
// "username": "<user-username>",
// "password": "<user-password>"
// }
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
let json = chilkat::JsonObject::new();
let _ = json.update_string("client_id", "administration");
let _ = json.update_string("grant_type", "password");
let _ = json.update_string("scopes", "write");
let _ = json.update_string("username", "<user-username>");
let _ = json.update_string("password", "<user-password>");
let resp = chilkat::HttpResponse::new();
if http.http_json("POST", "https://my-shopware-6-shop.de/api/oauth/token", &json, "application/json", &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
let sb_response_body = chilkat::StringBuilder::new();
let _ = resp.get_body_sb(&sb_response_body);
let j_resp = chilkat::JsonObject::new();
let _ = j_resp.load_sb(&sb_response_body);
j_resp.set_emit_compact(false);
println!("Response Body:");
println!("{}", j_resp.emit().unwrap_or_default());
let resp_status_code = resp.status_code();
println!("Response Status Code = {}", resp_status_code);
if resp_status_code >= 400 {
println!("Response Header:");
println!("{}", resp.header());
println!("Failed.");
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "token_type": "Bearer",
// "expires_in": 600,
// "access_token": "xxxxxxxxxxxxxx",
// "refresh_token": "token"
// }
// 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 token_type = j_resp.string_of("token_type").unwrap_or_default();
let expires_in = j_resp.int_of("expires_in");
let access_token = j_resp.string_of("access_token").unwrap_or_default();
let refresh_token = j_resp.string_of("refresh_token").unwrap_or_default();
// Save our JSON to a file (to be used in other examples..)
let _ = j_resp.write_file("qa_data/tokens/shopware6.json").is_ok();