[GH-ISSUE #318] Help wanted with AuthCodeSpotify auth flow #98

Closed
opened 2026-02-27 20:23:06 +03:00 by kerem · 3 comments
Owner

Originally created by @Staninna on GitHub (May 24, 2022).
Original GitHub issue: https://github.com/ramsayleung/rspotify/issues/318

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

Originally created by @Staninna on GitHub (May 24, 2022). Original GitHub issue: https://github.com/ramsayleung/rspotify/issues/318 Hello i am trying to auth with the AuthCodeSpotify but I just cant figure it out I like a little help ```rs // 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](https://github.com/Staninna/SpotiAFK-v2) I hope i didn't break any rules by opening this issue but i really need support
kerem 2026-02-27 20:23:06 +03:00
Author
Owner

@ramsayleung commented on GitHub (May 24, 2022):

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 :)

<!-- gh-comment-id:1135955199 --> @ramsayleung commented on GitHub (May 24, 2022): 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](https://github.com/ramsayleung/rspotify/blob/master/examples/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 :)
Author
Owner

@Staninna commented on GitHub (May 24, 2022):

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

<!-- gh-comment-id:1135976568 --> @Staninna commented on GitHub (May 24, 2022): This is what I get when I try to run [auth_code.rs](https://github.com/ramsayleung/rspotify/blob/master/examples/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](https://docs.rs/rspotify/latest/rspotify/auth_code/struct.AuthCodeSpotify.html) in steps 3 and 4
Author
Owner

@Staninna commented on GitHub (May 24, 2022):

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

<!-- gh-comment-id:1136004868 --> @Staninna commented on GitHub (May 24, 2022): I fixed it. I didn't know crates had features. Now I do, and enabling the `cli` feature fixed my problem
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/rspotify#98
No description provided.