Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

google oauth2 refresh token #62

Closed
mrdotb opened this issue May 29, 2019 · 4 comments
Closed

google oauth2 refresh token #62

mrdotb opened this issue May 29, 2019 · 4 comments

Comments

@mrdotb
Copy link

mrdotb commented May 29, 2019

Hello, thanks for the library.

I encounter a problem. I'm making a cmd line app who use googlecloudapi. I want to cache the token and refresh it each time the app is launched.
My problem is google do not send a new refresh_token or the previous one when you renew. So the lib erase the old refresh_token field. So my idea was to manually call set_refresh_token but I can't access the refresh_token field.

fn cache(token: BasicTokenResponse) -> Result<BasicTokenResponse, TokenError> {
    // Save serialized token
    let serialized_json = serde_json::to_string(&token)?;
    let mut file = File::create("token.json")?;
    file.write_all(serialized_json.as_bytes())?;
    Ok(token)
}

fn check_cache() -> Result<BasicTokenResponse, TokenError> {
    let mut file = File::open("token.json")?;
    let mut serialized_json = String::new();

    file.read_to_string(&mut serialized_json)?;
    let token: BasicTokenResponse = serde_json::from_str(&serialized_json)?;

    let refresh_token = token.refresh_token().unwrap();
    let mut fresh_token = create_client()?
        .exchange_refresh_token(refresh_token)
        .unwrap();


    if fresh_token.access_token().secret() != token.access_token().secret() {
        //Set the previous refresh_token
        fresh_token.set_refresh_token(Some(RefreshToken::new("ccc".to_string())));
        cache(fresh_token)
    } else {
        Ok(token)
    }
}

Call result

// First call
{
  "access_token": "XXXXXXX",
  "token_type": "bearer",
  "expires_in": 3600,
  "refresh_token": "XXXXXXX",
  "scope": "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/documents"
}
// Using the refresh token
{
  "access_token": "XXXXXXX",
  "token_type": "bearer",
  "expires_in": 3600,
  "scope": "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/documents"
}
@ramosbugs
Copy link
Owner

does this work?

fresh_token.set_refresh_token(Some(refresh_token));

I don't fully understand the but I can't access the refresh_token field part of your question. The (old) refresh token is already in scope above in a variable called refresh_token.

@mrdotb
Copy link
Author

mrdotb commented May 29, 2019

Thanks for the fast answer. This is my first project with rust.
No it does not work.
refresh_token fn give me a reference (&RefreshToken) but the set function want a RefreshToken.
Is it possible to access the string behind &RefreshToken ?
From the lib

pub fn refresh_token(&self) -> Option<&RefreshToken> {
    self.refresh_token.as_ref()
 }

 pub fn set_refresh_token(&mut self, refresh_token: Option<RefreshToken>) {
   self.refresh_token = refresh_token;
 }

@ramosbugs
Copy link
Owner

oh I see. I think what you want to do here is clone the refresh token since set_refresh_token() needs an owned value. RefreshToken implements Clone, so you should just be able to do:

fresh_token.set_refresh_token(Some(refresh_token.clone()));

@mrdotb
Copy link
Author

mrdotb commented May 29, 2019

Thanks for your help
I reread rust doc about references and borrowing and just find it worked with to_owned()
fresh_token.set_refresh_token(Some(refresh_token.to_owned()));

@mrdotb mrdotb closed this as completed May 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants