use std::future::Future;
use std::pin::Pin;
use yup_oauth2::authenticator_delegate::{DefaultInstalledFlowDelegate, InstalledFlowDelegate};
async fn browser_user_url(url: &str, need_code: bool) -> Result<String, String> {
if webbrowser::open(url).is_ok() {
println!("webbrowser was successfully opened.");
}
let def_delegate = DefaultInstalledFlowDelegate;
def_delegate.present_user_url(url, need_code).await
}
#[derive(Copy, Clone)]
struct InstalledFlowBrowserDelegate;
impl InstalledFlowDelegate for InstalledFlowBrowserDelegate {
fn present_user_url<'a>(
&'a self,
url: &'a str,
need_code: bool,
) -> Pin<Box<dyn Future<Output = Result<String, String>> + Send + 'a>> {
Box::pin(browser_user_url(url, need_code))
}
}
#[tokio::main]
async fn main() {
let sec = yup_oauth2::read_application_secret("client_secret.json")
.await
.expect("client secret couldn't be read.");
let auth = yup_oauth2::InstalledFlowAuthenticator::builder(
sec,
yup_oauth2::InstalledFlowReturnMethod::HTTPRedirect,
)
.persist_tokens_to_disk("tokencache.json")
.flow_delegate(Box::new(InstalledFlowBrowserDelegate))
.build()
.await
.expect("InstalledFlowAuthenticator failed to build");
let scopes = &["https://www.googleapis.com/auth/drive.file"];
match auth.token(scopes).await {
Err(e) => println!("error: {:?}", e),
Ok(t) => println!("The token is {:?}", t),
}
}