Getting Oauth2 token in Rust

47 views Asked by At

When running this code

pub async fn oauth_return(
    Query(query): Query<AuthRequest>,
    State(state): State<AppState>,
    Host(hostname): Host,
) -> Result<impl IntoResponse, ReturnError> {
    // Get an auth token
    let token = state.oauth_client
        .exchange_code(AuthorizationCode::new(query.code.clone()))
        .request_async(async_http_client)
        .await
        .context("failed in sending request request to authorization server")?;
.
.
.

I get the following server error, even though it compiles just fine

2024-03-26T20:40:52.505292Z ERROR rustapi::handlers::login_service: Application error: failed in sending request request to authorization server: Failed to parse server response: expected value at line 1 column 1

I have look at the Oauth example in the Axum documentation and this is the exact code it refrences. Other public repos I looked at also use the same code. The breakpoint in debug had a code stored in the query that I should be able to get but I dont know why it wont work

Here is how im intializing the client

pub(crate) fn oauth_client() -> Result<BasicClient, crate::types::OauthError> {
    let client_id = ClientId::new(env::var("CLIENT_ID")?);
    let client_secret = ClientSecret::new(env::var("CLIENT_SECRET")?);
    let redirect_url = "http://127.0.0.1:8080/auth/authorized".to_string();

    let auth_url = AuthUrl::new("https://accounts.google.com/o/oauth2/auth".to_string())
        .map_err(|_| "OAuth: invalid authorization endpoint URL")?;

    let token_url = TokenUrl::new("https://www.googleapis.com/oauth2/token".to_string())
        .map_err(|_| "OAuth: invalid token endpoint URL")?;




    let client = BasicClient::new(
        client_id,
        Some(client_secret),
        auth_url,
        Some(token_url),
    )
        .set_redirect_uri(RedirectUrl::new(redirect_url).map_err(|_| "OAuth: invalid redirect URL")?)
        .set_revocation_uri(
            RevocationUrl::new("https://oauth2.googleapis.com/revoke".to_string())
                .map_err(|_| "OAuth: invalid revocation endpoint URL")?,
        );
    Ok(client)
}
0

There are 0 answers