pub struct Credentials {
pub client_secrets: ClientSecrets,
pub access_token: AccessToken,
}Expand description
Contains the credentials ( ClientSecrets and
AccessToken ) for a authorizing requests to the Google Drive API using
OAuth 2.0.
§Note:
When creating credentials a client_secrets JSON file is required in order to use OAuth2 to authorize your API.
If you do not have a client_secrets JSON file, follow
these steps, once you complete the
Authorize credentials for a desktop application
section you can use the downloaded secrets file.
§Examples
use drive_v3::Credentials;
let secrets_path = "my_client_secrets.json";
let scopes = ["https://www.googleapis.com/auth/drive.metadata.readonly"];
let credentials = Credentials::from_client_secrets_file(&secrets_path, &scopes)?;
assert!( credentials.are_valid() );Fields§
§client_secrets: ClientSecretsRepresentation of the client_secret.json file that contains the authorization info for your API.
access_token: AccessTokenToken used to authenticate and provide authorization information to Google APIs.
Implementations§
Source§impl Credentials
impl Credentials
Sourcepub fn new(client_secrets: &ClientSecrets, access_token: &AccessToken) -> Self
pub fn new(client_secrets: &ClientSecrets, access_token: &AccessToken) -> Self
Creates new Credentials using client_secrets and access_token.
Examples:
use drive_v3::AccessToken;
use drive_v3::{Credentials, Drive};
use drive_v3::ClientSecrets;
// Load your client_secrets file
let secrets_path = "my_client_secrets.json";
let my_client_secrets = ClientSecrets::from_file(secrets_path)?;
// Request an access token, this will prompt you to authorize via the browser
let scopes = ["https://www.googleapis.com/auth/drive.metadata.readonly"];
let my_access_token = AccessToken::request(&my_client_secrets, &scopes)?;
// Create Credentials
let my_credentials = Credentials::new(&my_client_secrets, &my_access_token);
// now you can create a Drive to make requests
let drive = Drive::new(&my_credentials);
let files = drive.files.list();
println!("files in drive: {:#?}", files);
Sourcepub fn get_access_token(&self) -> String
pub fn get_access_token(&self) -> String
Gets the access token which is sent as a HTTP Authorization request header to the Google Drive API to authenticate access.
Examples:
// You can use this token to make a requests (with reqwest for example) using it for authorization
let client = reqwest::blocking::Client::new();
let body = client.get("google-api-endpoint")
.bearer_auth( &credentials.get_access_token() )
.send()?
.text()?;
println!("response: {:?}", body);Sourcepub fn store<T: AsRef<Path>>(&self, path: T) -> Result<()>
pub fn store<T: AsRef<Path>>(&self, path: T) -> Result<()>
Saves Credentials as a JSON file in path.
§Examples
use drive_v3::Credentials;
// Store credentials for the next time you need authorization
credentials.store("credentials_new.json")?;
let required_scopes = ["https://www.googleapis.com/auth/drive.metadata.readonly"];
let stored_credentials = Credentials::from_file("credentials_new.json", &required_scopes)?;
assert_eq!(credentials, stored_credentials);§Errors
- a
Jsonerror, if unable to parse theCredentialsto JSON. - an
IOerror, if unable to write topath.
Sourcepub fn refresh(&mut self) -> Result<()>
pub fn refresh(&mut self) -> Result<()>
Refreshes expired or invalid Credentials.
§Note
The refreshed credentials are updated in place, you should save them to a file using the store
function to avoid requesting to many new credentials.
§Examples
if !credentials.are_valid() {
credentials.refresh()?;
}
assert!( credentials.are_valid() );§Errors
Sourcepub fn are_valid(&self) -> bool
pub fn are_valid(&self) -> bool
Checks if these Credentials are valid by making a request to the
tokeninfo endpoint of
the Google Drive API.
§Note
are_valid will return false if the credentials have expired or have been revoked, but it will
also return false if the tokeninfo endpoint cannot be reached or if the request returned an error response of any kind.
§Examples
if credentials.are_valid() {
println!("credentials are valid!, you can start making requests");
} else {
println!("credentials are invalid :c, try calling 'credentials.refresh()' to update them");
}Sourcepub fn from_file<T, U>(file: T, scopes: &[U]) -> Result<Self>
pub fn from_file<T, U>(file: T, scopes: &[U]) -> Result<Self>
Retrieves previously stored Credentials from file and checks that they have the required scopes enabled.
§Note
from_file only parses the stored credentials it does not validate them, before using them to
make requests you must ensure that they are still valid using the are_valid function.
If they are no longer valid use the refresh function to update them.
§Examples
use drive_v3::Credentials;
let credentials_path = "my_credentials.json";
let scopes = ["https://www.googleapis.com/auth/drive.metadata.readonly"];
let credentials = Credentials::from_file(&credentials_path, &scopes)?;§Errors
- an
IOerror, if the file does not exist. - a
Jsonerror, if parsing of the file from JSON failed. - a
MismatchedScopeserror, if the scopes in the access token are different to thescopespassed to the function.
Sourcepub fn from_client_secrets_file<T, U>(file: T, scopes: &[U]) -> Result<Self>
pub fn from_client_secrets_file<T, U>(file: T, scopes: &[U]) -> Result<Self>
Attempts to get Credentials by prompting the user for authorization.
§Note:
A client_secrets json file is required in order to use OAuth2 to authorize your API.
If you do not have a client_secrets json file, follow
these steps, once you complete the
Authorize credentials for a desktop application
section you can use the downloaded secrets file.
§Examples
use drive_v3::Credentials;
let secrets_path = "my_client_secrets.json";
let scopes = ["https://www.googleapis.com/auth/drive.metadata.readonly"];
let credentials = Credentials::from_client_secrets_file(&secrets_path, &scopes)?;
assert!( credentials.are_valid() );§Errors
- an
IOorJsonerror, if unable getClientSecretsfromfile. - a
HexDecoding,UrlParsing,RequestorResponseerror, if the get access token request fails. - a
MismatchedScopeserror, if the scopes in the created access token are different to thescopespassed to the function.
Trait Implementations§
Source§impl Clone for Credentials
impl Clone for Credentials
Source§fn clone(&self) -> Credentials
fn clone(&self) -> Credentials
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Credentials
impl Debug for Credentials
Source§impl<'de> Deserialize<'de> for Credentials
impl<'de> Deserialize<'de> for Credentials
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for Credentials
impl PartialEq for Credentials
Source§impl Serialize for Credentials
impl Serialize for Credentials
impl Eq for Credentials
impl StructuralPartialEq for Credentials
Auto Trait Implementations§
impl Freeze for Credentials
impl RefUnwindSafe for Credentials
impl Send for Credentials
impl Sync for Credentials
impl Unpin for Credentials
impl UnwindSafe for Credentials
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.