dermesser / yup-oauth2

An oauth2 client implementation providing the Device, Installed, Service Account, and several more flows.
https://docs.rs/yup-oauth2/
Apache License 2.0
213 stars 114 forks source link

Is it possible to use static API key for simple usage #218

Closed SirMishaa closed 1 month ago

SirMishaa commented 2 months ago

Hello,

I'd like to interact with Google Calendar API to create events. It's only for myself for now, and I do not need OAuth 2.0 and the infrastructure that it needs (having a server that can receive the token, etc).

I'm wondering if it's possible to use static API key ? See the following : https://github.com/Byron/google-apis-rs/issues/172

Sorry If I miss something in the documentation. Best regards.

Edit: I have tried the following (the access token is valid and have an access to my calendar) :

use google_calendar3::{oauth2, CalendarHub};

let auth = oauth2::authenticator::AccessTokenAuthenticator::builder(
        "<REDACTED>".to_string(),
    )
    .build()
    .await?;

    let hub = CalendarHub::new(
        hyper::Client::builder().build(
            hyper_rustls::HttpsConnectorBuilder::new()
                .with_native_roots()
                .https_only()
                .enable_http2()
                .build(),
        ),
        auth,
    );

    let calendar = hub
        .calendars()
        .get("<REDACTED>")
        .doit()
        .await?;
    println!("Calendar ID: {:?}", calendar.1.id);

I got the following error from google :

[2024-05-06T03:18:49Z DEBUG rustls::common_state] Sending warning alert CloseNotify Error: BadRequest(Object {"error": Object {"code": Number(401), "errors": Array [Object {"domain": String("global"), "location": String("Authorization"), "locationType": String("header"), "message": String("Invalid Credentials"), "reason": String("authError")}], "message": String("Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project."), "status": String("UNAUTHENTICATED")}})

SirMishaa commented 2 months ago

After some debugging, the request seems good. (I mean, there is a Bearer token in header when using AccessTokenAuthenticator)

It looks like Google does not allow connecting using API KEY. Only OAuth 2.0 token seems to be accepted 😐. If you don't have more information, you can close the issue :)

Thank you.