diff --git a/examples/google.rs b/examples/google.rs index 176964d..a2338df 100644 --- a/examples/google.rs +++ b/examples/google.rs @@ -24,4 +24,8 @@ fn main() { let token = client.request_token(code.trim()).unwrap(); println!("{:?}", token); + + let refreshed = client.refresh_token(&token, None).unwrap(); + + println!("{:?}", refreshed); } diff --git a/src/client.rs b/src/client.rs index 7e820ff..de831f6 100644 --- a/src/client.rs +++ b/src/client.rs @@ -173,23 +173,13 @@ impl Client { ]) } - /// Requests an access token using an authorization code. - pub fn request_token(&self, code: &str) -> Result { - let mut body_pairs = vec![ - ("grant_type", "authorization_code"), - ("code", code), - ]; - if let Some(ref redirect_uri) = self.redirect_uri { - body_pairs.push(("redirect_uri", redirect_uri)); - } - - let body_str = form_urlencoded::serialize(body_pairs); - + fn token_post(&self, body_pairs: Vec<(&str, &str)>) -> Result { + let post_body = form_urlencoded::serialize(body_pairs); let request = self.http_client.post(&self.token_uri) .header(self.auth_header()) .header(self.accept_header()) .header(header::ContentType::form_url_encoded()) - .body(&body_str); + .body(&post_body); let mut response = try!(request.send()); let mut body = String::new(); @@ -203,4 +193,43 @@ impl Client { let error: ErrorResponse = try!(json::decode(&body)); Err(Error::OAuth2(error.into())) } + + /// Requests an access token using an authorization code. + pub fn request_token(&self, code: &str) -> Result { + let mut body_pairs = vec![ + ("grant_type", "authorization_code"), + ("code", code), + ]; + if let Some(ref redirect_uri) = self.redirect_uri { + body_pairs.push(("redirect_uri", redirect_uri)); + } + self.token_post(body_pairs) + } + + /// Refreshes an access token. + /// + /// # Panics + /// + /// Panics if `token` does not contain a `refresh_token`. + pub fn refresh_token(&self, token: &Token, scope: Option<&str>) -> Result { + let refresh_token = token.refresh_token.as_ref().unwrap(); + + let mut body_pairs = vec![ + ("grant_type", "refresh_token"), + ("refresh_token", refresh_token), + ]; + if let Some(scope) = scope { + body_pairs.push(("scope", scope)); + } + + let mut result = self.token_post(body_pairs); + + if let Ok(ref mut token) = result { + if token.refresh_token.is_none() { + token.refresh_token = Some(refresh_token.clone()); + } + } + + result + } }