Sample code for 30+ languages & platforms
Rust

Refresh WiX Access Token

See more WiX Examples

Request a new access token each time you call a WiX API. Use the refresh token together with your secret key, to request refresh tokens

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

// Implements the following CURL command:

// curl -X POST \
//   https://www.wix.com/oauth/access \
//   -H 'Content-Type: application/json' \
//   -d '{
//     "grant_type": "refresh_token",
//     "client_id": <CLIENT_ID>,
//     "client_secret": <CLIENT_SECRET>,
//     "refresh_token": <REFRESH_TOKEN>
// }'

// It is assumed we previously obtained an OAuth2 access token.
// This example loads the JSON access token file 
// saved by this example: Get WiX OAuth2 Access Token

let json_token = chilkat::JsonObject::new();
if json_token.load_file("qa_data/tokens/wix.json").is_err() {
    println!("Failed to load square.json");
    return;
}

// Get the "refresh_token"
let refresh_token = json_token.string_of("refresh_token").unwrap_or_default();

// The following JSON is sent in the request body.

// {
//   "grant_type": "refresh_token",
//   "client_id": <APP_ID>,
//   "client_secret": <APP_SECRET>,
//   "refresh_token": <REFRESH_TOKEN>
// }

let json = chilkat::JsonObject::new();
let _ = json.update_string("grant_type", "refresh_token");
let _ = json.update_string("client_id", "CLIENT_ID");
let _ = json.update_string("client_secret", "CLIENT_SECRET");
let _ = json.update_string("refresh_token", &refresh_token);

let resp = chilkat::HttpResponse::new();
if http.http_json("POST", "https://www.wix.com/oauth/access", &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:

// {
//   "refresh_token": "OAUTH2.eyJraWQ ... vnB4cQ",
//   "access_token": "OAUTH2.eyJra ... la18lrw"
// }

let refresh_token = j_resp.string_of("refresh_token").unwrap_or_default();
let access_token = j_resp.string_of("access_token").unwrap_or_default();

// Save the new JSON access token response to a file.
let _ = sb_response_body.write_file("qa_data/tokens/wix.json", "utf-8", false);