drive-v3 0.4.2

A library for interacting the Google Drive API
Documentation

drive-v3

pipeline coverage

A Rust library to send request to the Google Drive v3 API.

Warning

As of right now the only supported endpoints are about and files.

Usage

use std::path::PathBuf;
use drive_v3::{Error, Credentials, Drive};

fn main() -> Result<(), Error> {
    // Path to the client_secrets file created when enabling the Drive API in Google Cloud
    // if you do not have a client secrets file, follow the steps to setup an environment in this link:
    // https://developers.google.com/drive/api/quickstart/python#set_up_your_environment
    // after completing 'Authorize credentials for a desktop application' you can save the secrets file
    let client_secrets = "my_client_secrets.json";

    // Path where created credentials will be stored
    // if there are no stored credentials the user will be prompted to authorize via the browser when
    // the user authorizes the app the credentials will be stored in this path
    let credentials_storage = PathBuf::from("my_super_secret_credentials.json");

    // The scopes that the API can access
    // these are the only scopes currently used in the library, if you just want to read the files in
    // a drive only the first one is required
    let my_scopes: [&'static str; 2] = [
        "https://www.googleapis.com/auth/drive.metadata.readonly",
        "https://www.googleapis.com/auth/drive.file",
    ];

    let mut credentials = None;

    // Check if there are stored credentials
    if credentials_storage.exists() {
        let mut stored_credentials = Credentials::from_file(&credentials_storage, &my_scopes)?;

        // Refresh the credentials if they have expired
        if !stored_credentials.are_valid() {
            stored_credentials.refresh()?;

            // Save them so we don't have to refresh them every time
            stored_credentials.store(&credentials_storage)?;
        }

        // Set the credentials
        credentials = Some(stored_credentials);
    }

    // We'll have to prompt the user for authorization if the stored credentials don't work
    if credentials.is_none() {
        let created_credentials = Credentials::from_client_secrets_file(&client_secrets, &my_scopes)?;

        // Save them so we don't have to prompt the user again
        created_credentials.store(&credentials_storage)?;

        // Set the credentials
        credentials = Some(created_credentials);
    }

    // Create a Drive
    // this class makes all the request to the Google Drive API
    let my_drive = Drive::new( &credentials.unwrap() );

    // Lets list the files in a Drive
    let file_list = my_drive.files.list()
        .fields("files(name, id, mimeType)") // Set what fields will be returned
        .q("name = 'file_im_looking_for' and not trashed") // search for specific files
        .execute()?;

    if let Some(files) = file_list.files {
       for file in &files {
            println!("{}", file);
        }
    }

    Ok(())
}

Contributing

Pull requests are welcome. For major changes, please open an issue first.

License

MIT