ramsayleung / rspotify

Spotify Web API SDK implemented on Rust
MIT License
639 stars 123 forks source link

Help wanted with AuthCodeSpotify auth flow #318

Closed Staninna closed 2 years ago

Staninna commented 2 years ago

Hello i am trying to auth with the AuthCodeSpotify but I just cant figure it out I like a little help

// Used scopes
let scopes = scopes!("user-read-currently-playing");
// initialization of client
let credentials = Credentials::from_env().unwrap();
let oauth = OAuth::from_env(scopes).unwrap();
let mut client = AuthCodeSpotify::new(credentials, oauth);
// Get authorize url
let url = client.get_authorize_url(true).unwrap();
// Print messages to user
println!(
    "A webpage should open log into your spotify account and paste the redirected url here",
);
println!(
    "If no webpage opened, please open this url in your browser:\n\x1b[32m{}\x1b[0m",
    url.trim()
);
// Let user login
let redirect = input("Paste the redirected URL: ");
// Open url in default browser
open::that(url).unwrap();
// Get token
let response = client.parse_response_code(&redirect);
match response {
    #[allow(unused_must_use)]
    Some(code) => {
        client.request_token(code.as_str());
        client.read_token_cache(false);
        return Some(client);
    }
    None => None,
}

I struggle fingering out how to obtain the token If you want to see all my code i have a repository here

I hope i didn't break any rules by opening this issue but i really need support

ramsayleung commented 2 years ago

There are some working examples in this repository, would you like to take a look at them first? Such as this working example auth_code.rs which shows how to auth with AuthCode.

I struggle fingering out how to obtain the token

You should provide more details about what exactly went wrong, what's the error message. I have no idea what's going on, what are you struggling for, with a vague description :)

Staninna commented 2 years ago

This is what I get when I try to run auth_code.rs

Besides that, it doesn't run in the main function, and I had to move it to an async function because the main function can not be async. Maybe I am missing something because I am pretty new to rust

error[E0599]: no method named `prompt_for_token` found for struct `AuthCodeSpotify` in the current scope
  --> src/bin/test.rs:40:13
   |
40 |     spotify.prompt_for_token(&url).await.unwrap();
   |             ^^^^^^^^^^^^^^^^ method not found in `AuthCodeSpotify`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `spoti_afk` due to previous error

And what I meant by struggling with the way to obtain a token is the code flow on this doc page in steps 3 and 4

Staninna commented 2 years ago

I fixed it. I didn't know crates had features. Now I do, and enabling the cli feature fixed my problem