Rust
Rust
Configure OAuth 1.0a Authentication for REST
See more REST Examples
Demonstrates Rest.SetAuthOAuth1, which associates an OAuth1 provider so Chilkat computes the OAuth 1.0/1.0a signature for each request. The second argument selects whether OAuth parameters are placed in the query string or the Authorization header.
Background. OAuth 1.0a signs each request using the consumer key/secret and the access token/secret. Chilkat generates the nonce, timestamp, and signature automatically for every request sent through the object.
Chilkat Rust Downloads
let rest = chilkat::Rest::new();
let b_tls = true;
let b_auto_reconnect = true;
if rest.connect("example.com", 443, b_tls, b_auto_reconnect).is_err() {
println!("{}", rest.last_error_text());
return;
}
// Configure OAuth 1.0a request signing. Provide the consumer credentials and the access token
// credentials; Chilkat computes the OAuth signature for each request.
let oauth1 = chilkat::OAuth1::new();
// Normally you would not hard-code secrets in source. You should instead obtain them from an
// interactive prompt, environment variable, or a secrets vault.
oauth1.set_consumer_key("CONSUMER_KEY");
oauth1.set_consumer_secret("CONSUMER_SECRET");
oauth1.set_token("ACCESS_TOKEN");
oauth1.set_token_secret("ACCESS_TOKEN_SECRET");
oauth1.set_signature_method("HMAC-SHA1");
// The 2nd argument selects whether OAuth parameters are placed in the query string (true) or the
// Authorization header (false).
let b_use_query_params = false;
if rest.set_auth_o_auth1(&oauth1, b_use_query_params).is_err() {
println!("{}", rest.last_error_text());
return;
}
let Ok(response_text) = rest.full_request_no_body("GET", "/api/resource") else {
println!("{}", rest.last_error_text());
return;
};
println!("{}", response_text);