0

I am new to rust programming and trying to make an email summarizer, I have authenticated it but now when I am calling messages_get function I am getting the following error. I am using google_gmail1 crate

Here is the error :

Bad Request: {"error":{"code":403,"errors":[{"domain":"global","message":"Missing access token for authorization. Request: MailboxService.GetMessage","reason":"forbidden"}],"message":"Missing access token for authorization. Request: MailboxService.GetMessage","status":"PERMISSION_DENIED"}}

Here is my code:

use yup_oauth2::{InstalledFlowAuthenticator, InstalledFlowReturnMethod};
use tokio::io::{stdout, AsyncWriteExt as _};

extern crate google_gmail1 as gmail1;
use gmail1::api::Message;
use gmail1::{Result, Error};
use std::fs;
use gmail1::{Gmail, oauth2, hyper , hyper_rustls , chrono, FieldMask};

#[tokio::main]

async fn main() {
    // Read application secret from a file. Sometimes it's easier to compile it directly into
    // the binary. The clientsecret file contains JSON like `{"installed":{"client_id": ... }}`
    let secret = yup_oauth2::read_application_secret("D:\\email-summarizer\\src\\client_secret.json")
        .await
        .expect("D:\\email-summarizer\\src\\client_secret.json");

    // Create an authenticator that uses an InstalledFlow to authenticate. The
    // authentication tokens are persisted to a file named tokencache.json. The
    // authenticator takes care of caching tokens to disk and refreshing tokens once
    // they've expired.
    let  auth = InstalledFlowAuthenticator::builder(secret, InstalledFlowReturnMethod::HTTPRedirect)
    .persist_tokens_to_disk("tokencache.json")
    .build()
    .await
    .unwrap() ;

    let scopes = &["https://www.googleapis.com/auth/drive.file"];

    let mut hub = Gmail::new(
        hyper::Client::builder().build(
            hyper_rustls::HttpsConnectorBuilder::new().with_native_roots().https_or_http().enable_http1().enable_http2().build()),
             auth);
    let mut req = Message::default();
    
    let result =hub.users().messages_get("me" , "xxxx")
    .param("access_token" , "xxxxx")
    .doit().await;

    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::HttpError(_)
        |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),
}
}

I have tried adding api key, refresh token but all results in same error. I am getting my access token from tokencache.json file , maybe thats wrong, Please guide

glitch_123
  • 139
  • 12

0 Answers0