You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by su...@apache.org on 2023/07/07 09:18:03 UTC
[incubator-opendal] 01/03: refresh token flow
This is an automated email from the ASF dual-hosted git repository.
suyanhanx pushed a commit to branch dropbox-batch-delete
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git
commit f4747b63f990a01e7eb47c572c461db10dc62e0b
Author: suyanhanx <su...@gmail.com>
AuthorDate: Fri Jul 7 14:33:16 2023 +0800
refresh token flow
Signed-off-by: suyanhanx <su...@gmail.com>
---
core/src/services/dropbox/builder.rs | 47 +++++++++++++++++++++++++++---------
core/src/services/dropbox/core.rs | 5 ++--
2 files changed, 38 insertions(+), 14 deletions(-)
diff --git a/core/src/services/dropbox/builder.rs b/core/src/services/dropbox/builder.rs
index 2827d86a9..bbe1470f8 100644
--- a/core/src/services/dropbox/builder.rs
+++ b/core/src/services/dropbox/builder.rs
@@ -49,10 +49,32 @@ use crate::*;
///
/// # Configuration
///
-/// - `access_token`: set the access_token for google drive api
-/// - `root`: Set the work directory for backend
+/// - `root`: Set the work directory for this backend.
///
-/// You can refer to [`DropboxBuilder`]'s docs for more information
+/// ## Credentials related
+///
+/// ### Just provide Access Token (Temporary)
+///
+/// - `access_token`: set the access_token for this backend.
+/// Please notice its expiration.
+///
+/// ### Or provide Client ID and Client Secret and refresh token (Long Term)
+///
+/// If you want to let OpenDAL to refresh the access token automatically,
+/// please provide the following fields:
+///
+/// - `refresh_token`: set the refresh_token for dropbox api
+/// - `client_id`: set the client_id for dropbox api
+/// - `client_secret`: set the client_secret for dropbox api
+///
+/// OpenDAL is a library, it cannot do the first step of OAuth2 for you.
+/// You need to get authorization code from user by calling Dropbox's authorize url
+/// and exchange it for refresh token.
+///
+/// Please refer to [Dropbox OAuth2 Guide](https://www.dropbox.com/developers/reference/oauth-guide)
+/// for more information.
+///
+/// You can refer to [`DropboxBuilder`]'s docs for more information.
///
/// # Example
///
@@ -66,8 +88,8 @@ use crate::*;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
-/// // create backend builder
/// let mut builder = Dropbox::default();
+/// builder.root("/test");
/// builder.access_token("<token>");
///
/// let op: Operator = Operator::new(builder)?.finish();
@@ -78,7 +100,9 @@ use crate::*;
#[derive(Default)]
pub struct DropboxBuilder {
root: Option<String>,
+
access_token: Option<String>,
+
refresh_token: Option<String>,
client_id: Option<String>,
client_secret: Option<String>,
@@ -105,15 +129,16 @@ impl DropboxBuilder {
///
/// You can get the access token from [Dropbox App Console](https://www.dropbox.com/developers/apps)
///
- /// NOTE: this token will be expired in 4 hours. If you are trying to use dropbox services in a long time, please set a refresh_token instead.
+ /// NOTE: this token will be expired in 4 hours.
+ /// If you are trying to use the Dropbox service in a long time, please set a refresh_token instead.
pub fn access_token(&mut self, access_token: &str) -> &mut Self {
self.access_token = Some(access_token.to_string());
self
}
- /// Refersh token is used for long term access to the Dropbox API.
+ /// Refresh token is used for long term access to the Dropbox API.
///
- /// You can get the refresh token via OAuth2.0 Flow of dropbox.
+ /// You can get the refresh token via OAuth 2.0 Flow of Dropbox.
///
/// OpenDAL will use this refresh token to get a new access token when the old one is expired.
pub fn refresh_token(&mut self, refresh_token: &str) -> &mut Self {
@@ -121,17 +146,17 @@ impl DropboxBuilder {
self
}
- /// Set the client id for dropbox.
+ /// Set the client id for Dropbox.
///
- /// This is required for OAuth2.0 Flow with refresh token.
+ /// This is required for OAuth 2.0 Flow to refresh the access token.
pub fn client_id(&mut self, client_id: &str) -> &mut Self {
self.client_id = Some(client_id.to_string());
self
}
- /// Set the client secret for dropbox.
+ /// Set the client secret for Dropbox.
///
- /// This is required for OAuth2.0 Flow with refresh token.
+ /// This is required for OAuth 2.0 Flow with refresh the access token.
pub fn client_secret(&mut self, client_secret: &str) -> &mut Self {
self.client_secret = Some(client_secret.to_string());
self
diff --git a/core/src/services/dropbox/core.rs b/core/src/services/dropbox/core.rs
index bbb2e5208..07d67ecaa 100644
--- a/core/src/services/dropbox/core.rs
+++ b/core/src/services/dropbox/core.rs
@@ -175,7 +175,7 @@ impl DropboxCore {
if !signer.access_token.is_empty() && signer.expires_in > Utc::now() {
let value = format!("Bearer {}", signer.access_token)
.parse()
- .expect("token must be valid header");
+ .expect("token must be valid header value");
req.headers_mut().insert(header::AUTHORIZATION, value);
return Ok(());
}
@@ -202,7 +202,6 @@ impl DropboxCore {
serde_json::from_slice(&body).map_err(new_json_deserialize_error)?;
// Update signer after token refreshed.
-
signer.access_token = token.access_token.clone();
// Refresh it 2 minutes earlier.
@@ -211,7 +210,7 @@ impl DropboxCore {
let value = format!("Bearer {}", token.access_token)
.parse()
- .expect("token must be valid header");
+ .expect("token must be valid header value");
req.headers_mut().insert(header::AUTHORIZATION, value);
Ok(())