Sample code for 30+ languages & platforms
Rust

Shopware 6 - Get OAuth2 Access Token using Client Credentials (Integration)

See more Shopware 6 Examples

Demonstrates how to get an OAuth2 access token with an integration.

Chilkat Rust Downloads

Rust

// 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

// 	POST /api/oauth/token
// 	{
// 	    "grant_type": "client_credentials",
// 	    "client_id": "<client-id>",
// 	    "client_secret": "<client-secret>"
// 	}

// 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("grant_type", "client_credentials");
let _ = json.update_string("client_id", "client-id");
let _ = json.update_string("client_secret", "client-secret");

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();