DEV Community

Rohan Rajpal
Rohan Rajpal

Posted on

Rust + Google Sheets API Quickstart

Google sheets API is a very useful tool. For MVPs and prototypes it works amazing, no need for SQL setup and you have a very powerful database frontend (google sheets) to interact with.

Recently I've been learning Rust and wanted to work with Sheets API using Rust.

Sadly, the sheets documentation has no quickstart of Rust mentioned.

So this post, you'll learn how to use Sheets API with Rust.

cargo new sheets_api_rust
Enter fullscreen mode Exit fullscreen mode

Your Cargo.toml should look like:

name = "sheets_api_rust"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at

google-sheets4 = "*"
hyper = "^0.14"
hyper-rustls = "^0.22"
serde = "^1.0"
serde_json = "^1.0"
yup-oauth2 = "^5.0"
tokio = { version = "~1.2", features = [
] }
Enter fullscreen mode Exit fullscreen mode

and obtain your api keys from google.

your credentials.json should look like

  "installed": {
    "client_id": "",
    "project_id": "sanguine-rhythm-105020",
    "auth_uri": "",
    "token_uri": "",
    "auth_provider_x509_cert_url": "",
    "client_secret": "QeQUnhzsiO4t--ZGmj9muUAu",
    "redirect_uris": ["urn:ietf:wg:oauth:2.0:oob", "http://localhost"]
Enter fullscreen mode Exit fullscreen mode

and finally your

// This is a modified version of the example at:

extern crate google_sheets4 as sheets4;
extern crate hyper;
extern crate hyper_rustls;
extern crate yup_oauth2 as oauth2;
use sheets4::Error;
use sheets4::Sheets;

async fn main() {
    // Get an ApplicationSecret instance by some means. It contains the `client_id` and
    // `client_secret`, among other things.
    let secret = yup_oauth2::read_application_secret("clientsecret.json")
        .expect("client secret could not be read");

    // Instantiate the authenticator. It will choose a suitable authentication flow for you,
    // unless you replace  `None` with the desired Flow.
    // Provide your own `AuthenticatorDelegate` to adjust the way it operates and get feedback about
    // what's going on. You probably want to bring in your own `TokenStorage` to persist tokens and
    // retrieve them from storage.
    let auth = yup_oauth2::InstalledFlowAuthenticator::builder(

    let hub = Sheets::new(

    let result = hub
        .get("1TWUpZdjXiquf-LsfbqXEIBVWgZ12XeaZtzrNp3uaHX8") // your spreadsheet id enters here

    // println!("{:?}",result);
    match result {
        Err(e) => match e {
            // The Error enum provides details about what exactly happened.
            // You can also just use its `Debug`, `Display` or `Error` traits
            | Error::Io(_)
            | Error::MissingAPIKey
            | Error::MissingToken(_)
            | Error::Cancelled
            | Error::UploadSizeLimitExceeded(_, _)
            | Error::Failure(_)
            | Error::BadRequest(_)
            | Error::FieldClash(_)
            | Error::JsonDecodeError(_, _) => println!("{}", e),
        Ok(res) => println!("Success: {:?}", res),
Enter fullscreen mode Exit fullscreen mode

and boom, now you can explore the sheets4 docs to see what more you can do!



Top comments (2)

perja2 profile image

hi. i get this error:

error[E0308]: mismatched types
--> src/
34 | hyper::Client::builder().build(hyper_rustls::HttpsConnector::with_native_roots()),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct sheets4::hyper_rustls::HttpsConnector, found struct hyper_rustls::HttpsConnector