Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
sloganking opened this issue Mar 1, 2024 · 1 comment
Closed

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

sloganking opened this issue Mar 1, 2024 · 1 comment

Comments

@sloganking
Copy link
Contributor

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
Copy link
Owner

Bogpan commented Jul 27, 2024

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants