Skip to content

Commit

Permalink
print out the env variables to set in the demo apps after authorizing
Browse files Browse the repository at this point in the history
  • Loading branch information
wfraser committed Apr 23, 2024
1 parent cc3a385 commit a324511
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
10 changes: 8 additions & 2 deletions examples/demo-async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! the contents of a folder recursively, and fetching a file given its path.
use tokio_util::compat::FuturesAsyncReadCompatExt;
use dropbox_sdk::default_async_client::UserAuthDefaultClient;
use dropbox_sdk::default_async_client::{NoauthDefaultClient, UserAuthDefaultClient};
use dropbox_sdk::async_routes::files;

enum Operation {
Expand Down Expand Up @@ -61,7 +61,13 @@ async fn main() {
std::process::exit(1);
}

let auth = dropbox_sdk::oauth2::get_auth_from_env_or_prompt();
let mut auth = dropbox_sdk::oauth2::get_auth_from_env_or_prompt();
if auth.save().is_none() {
auth.obtain_access_token_async(NoauthDefaultClient::default()).await.unwrap();
eprintln!("Next time set these environment variables to reuse this authorization:");
eprintln!(" DBX_CLIENT_ID={}", auth.client_id());
eprintln!(" DBX_OAUTH={}", auth.save().unwrap());
}
let client = UserAuthDefaultClient::new(auth);

if let Operation::Download(path) = op {
Expand Down
10 changes: 8 additions & 2 deletions examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! This example illustrates a few basic Dropbox API operations: getting an OAuth2 token, listing
//! the contents of a folder recursively, and fetching a file given its path.
use dropbox_sdk::default_client::UserAuthDefaultClient;
use dropbox_sdk::default_client::{NoauthDefaultClient, UserAuthDefaultClient};
use dropbox_sdk::files;

use std::io;
Expand Down Expand Up @@ -61,7 +61,13 @@ fn main() {
std::process::exit(1);
}

let auth = dropbox_sdk::oauth2::get_auth_from_env_or_prompt();
let mut auth = dropbox_sdk::oauth2::get_auth_from_env_or_prompt();
if auth.save().is_none() {
auth.obtain_access_token(NoauthDefaultClient::default()).unwrap();
eprintln!("Next time set these environment variables to reuse this authorization:");
eprintln!(" DBX_CLIENT_ID={}", auth.client_id());
eprintln!(" DBX_OAUTH={}", auth.save().unwrap());
}
let client = UserAuthDefaultClient::new(auth);

if let Operation::Download(path) = op {
Expand Down
11 changes: 8 additions & 3 deletions src/oauth2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@ pub struct Authorization {
}

impl Authorization {
/// Get the client ID for this authorization.
pub fn client_id(&self) -> &str {
&self.client_id
}

/// Create a new instance using the authorization code provided upon redirect back to your app
/// (or via manual user entry if not using a redirect URI) after the user logs in.
///
Expand Down Expand Up @@ -583,9 +588,9 @@ impl TokenCache {
/// Set the current short-lived token to a specific provided value. Normally it should not be
/// necessary to call this function; the token should be obtained automatically using the
/// refresh token.
pub async fn set_access_token(&self, access_token: Arc<String>) {
let mut write = self.auth.write().await;
write.1 = access_token;
pub fn set_access_token(&self, access_token: String) {
let mut write = self.auth.write_blocking();
write.1 = Arc::new(access_token);
}
}

Expand Down

0 comments on commit a324511

Please sign in to comment.