From 88ad6d3e3e0ea6a737dd69d42cb8abe6243d59f1 Mon Sep 17 00:00:00 2001 From: Wim Velzeboer Date: Tue, 22 Sep 2026 11:37:59 +0100 Subject: [PATCH] SFDX Auth url completed --- examples/sfdx_url.rs | 61 +++++++++++++++++++++++++++++++++++++++ examples/sfdx_url_json.rs | 0 rustfmt.toml | 0 3 files changed, 61 insertions(+) create mode 100644 examples/sfdx_url.rs create mode 100644 examples/sfdx_url_json.rs create mode 100644 rustfmt.toml diff --git a/examples/sfdx_url.rs b/examples/sfdx_url.rs new file mode 100644 index 0000000..f003827 --- /dev/null +++ b/examples/sfdx_url.rs @@ -0,0 +1,61 @@ +//! RustSF Authentication library - SFDX Auth URL Example +//! +//! In this example, we demonstrate how to use the RustSF Authentication library to authenticate +//! with Salesforce using an SFDX auth URL. +//! The example includes the necessary imports, constants, and a main function that performs the +//! authentication and makes a REST API request. +//! +//! To create a SFDX Auth url, follow these steps: +//! - Download and install the SFDX CLI +//! - [Authenticate a Salesforce environment with the SFDX CLI](https://developer.salesforce.com/docs/platform/salesforce-cli-reference/guide/cli_reference_org_login_web.html) +//! ```bash +//! sf org login web --alias my-org-alias +//! ``` +//! - Export the SFDX Auth url from the CLI +//! ```bash +//! sf org auth show-sfdx-auth-url --target-org my-org-alias +//! # or +//! sf org auth show-sfdx-auth-url --target-org my-org-alias --json > sfdx_auth_url.json +//! ``` +//! +use oauth2::http::header::AUTHORIZATION; +use oauth2::http::{HeaderMap, HeaderValue}; +use rustsf_auth::credentials::SalesforceCredentials; + +pub const CONNECT_TIMEOUT: u64 = 15; +pub const REQUEST_TIMEOUT: u64 = 30; +#[tokio::main] +async fn main(){ + // The sfdx auth url, normally never hardcoded + let sfdx_auth_url = "force://PlatformCLI::5Aep8618kS0Qq2451324asdge5z2qiUtr6yudfghjfdfdSGz03Uv86qbbx2MOhcIyj.OTqFKFH.olbk5LdlYEAv@computing-platform.sandbox.my.salesforce.com"; + + // The Credentials configuration + let config = SalesforceCredentials::sfdx_url(sfdx_auth_url).unwrap(); + + // Constructing the authentication session and connecting to Salesforce + let session = config.connect().await.unwrap(); + + // Build the headers to include the access token + let mut headers = HeaderMap::new(); + let auth_value = format!("Bearer {}", session.access_token().await.unwrap()); + headers.insert(AUTHORIZATION, HeaderValue::from_str(&auth_value).unwrap()); + + // A REST API request + let response = reqwest::Client::builder() + .redirect(reqwest::redirect::Policy::none()) + .connect_timeout(std::time::Duration::from_secs(CONNECT_TIMEOUT)) + .timeout(std::time::Duration::from_secs(REQUEST_TIMEOUT)) + .build() + .unwrap() + .get(format!("{}/services/data", session.instance_url)) + .headers(headers.clone()) + .send() + .await + .unwrap(); + + if response.status().is_success() { + println!("SUCCESS Response: {:?}", response.text().await.unwrap()); + } else { + println!("ERROR Response: {:?}", response.text().await.unwrap()); + } +} \ No newline at end of file diff --git a/examples/sfdx_url_json.rs b/examples/sfdx_url_json.rs new file mode 100644 index 0000000..e69de29 diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..e69de29