Rust Requires Chilkat v11.0.0+
Rust
ETrade OAuth1 Authorization (3-legged) Step 2
See more ETrade Examples
Demonstrates the final step in 3-legged OAuth1 authorization for the ETrade REST API. Example uses the OAuth1 verifier code that was copy-and-pasted from the browser in the 1st step. The end result of this final OAuth1 step is an access token that can be used to make ETrade REST API calls.See https://apisb.etrade.com/docs/api/authorization/get_access_token.html
Chilkat Rust Downloads
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let consumer_key = "ETRADE_CONSUMER_KEY".to_string();
let consumer_secret = "ETRADE_CONSUMER_SECRET".to_string();
let request_token_url = "https://apisb.etrade.com/oauth/request_token".to_string();
let authorize_url = "https://us.etrade.com/e/t/etws/authorize".to_string();
let access_token_url = "https://apisb.etrade.com/oauth/access_token".to_string();
let http = chilkat::Http::new();
let _ = true;
http.set_o_auth1(true);
http.set_o_auth_consumer_key(&consumer_key);
http.set_o_auth_consumer_secret(&consumer_secret);
http.set_o_auth_callback("oob");
let json_request_token = chilkat::JsonObject::new();
let _ = json_request_token.load_file("qa_data/tokens/etrade_request_token.json").is_ok();
let request_token = json_request_token.string_of("oauth_token").unwrap_or_default();
let request_token_secret = json_request_token.string_of("oauth_token_secret").unwrap_or_default();
// ------------------------------------------------------------------------------
// Exchange the OAuth Request Token for an OAuth Access Token.
http.set_o_auth_token(&request_token);
http.set_o_auth_token_secret(&request_token_secret);
// This is the verifier that was interactively copy-and-pasted from the browser back to our app.
http.set_o_auth_verifier("NJ07S");
// Use the explicit string "INCLUDE_OAUTH_TOKEN" to tell Chilkat to include the "oauth_token" param in the Authorization header field
http.set_uncommon_options("INCLUDE_OAUTH_TOKEN");
let resp = chilkat::HttpResponse::new();
if http.http_no_body("GET", &access_token_url, &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
// Make sure a successful response was received.
if resp.status_code() != 200 {
println!("{}", resp.status_line());
println!("{}", resp.header());
println!("{}", resp.body_str());
return;
}
// If successful, the resp.BodyStr contains something like this:
// oauth_token=85123455-fF41296Bi3daM8eCo9Y5vZabcdxXpRv864plYPOjr&oauth_token_secret=afiYJOgabcdSfGae7BDvJVVTwys8fUGpra5guZxbmFBZo
println!("{}", resp.body_str());
let hash_tab = chilkat::Hashtable::new();
let _ = hash_tab.add_query_params(&resp.body_str());
let access_token = hash_tab.lookup_str("oauth_token").unwrap_or_default();
let access_token_secret = hash_tab.lookup_str("oauth_token_secret").unwrap_or_default();
// The access token + secret is what should be saved and used for
// subsequent REST API calls.
println!("Access Token = {}", access_token);
println!("Access Token Secret = {}", access_token_secret);
// Save this access token for future calls.
// Just in case we need user_id and screen_name, save those also..
let json = chilkat::JsonObject::new();
let _ = json.append_string("oauth_token", &access_token);
let _ = json.append_string("oauth_token_secret", &access_token_secret);
let fac = chilkat::FileAccess::new();
let _ = fac.write_entire_text_file("qa_data/tokens/etrade.json", &json.emit().unwrap_or_default(), "utf-8", false);
println!("Success.");