Bogpan / spotify-rs

Rust wrapper for the Spotify API.
https://crates.io/crates/spotify-rs
Apache License 2.0
10 stars 5 forks source link

Can't generate valid "auth_code" and "csrf_token" #7

Closed sloganking closed 3 months ago

sloganking commented 8 months ago

I am generating a URL for the user to log into via

use dotenvy::dotenv;
use spotify_rs::{AuthCodeClient, AuthCodeFlow, ClientCredsClient, ClientCredsFlow, RedirectUrl};
use std::{env, error::Error};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // This should match the redirect URI you set in your app's settings
    let redirect_url = RedirectUrl::new("http://localhost:3000".to_owned()).unwrap();
    let auto_refresh = true;
    let scopes = vec!["user-library-read", "playlist-read-private"];

    let _ = dotenv();
    let client_secret = env::var("spotify_client_secret").expect("CLIENT_SECRET must be set");
    let auth_code_flow =
        AuthCodeFlow::new("0079fab38fc74726ac22609e5f996898", client_secret, scopes);

    // Redirect the user to this URL to get the auth code and CSRF token
    let (client, url) = AuthCodeClient::new(auth_code_flow, redirect_url, auto_refresh);

    println!("Please visit this URL: {}", url);

After clicking the URL, and manually logging in, I am redirected to: http://localhost:3000/?code=...&state=... (where ... are codes.

I am passing code to "auth_code" and state to "csrf_token", in the

    let mut spotify = client
        .authenticate("auth_code", "csrf_token")
        .await
        .unwrap();

function. But as a result I get

`Err` value: InvalidStateParameter

Is this an error in this lib or am I doing something wrong?

Bogpan commented 3 months ago

Hey! Sorry for getting to it so late. I was indeed able to reproduce this, using io::stdin.read_line() to read the code. I assume you were using it too? Anyway, the issue in this case was that an \r\n was appended at the end of the code. It's fixed now. Thanks!