[GH-ISSUE #310] Refresh token example does not work #95

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

Originally created by @AndreKR on GitHub (Apr 12, 2022).
Original GitHub issue: https://github.com/ramsayleung/rspotify/issues/310

Describe the bug
The supplied examples/with_refresh_token.rs exits with an error.

To Reproduce

  • Create a new Rust project
  • Set up dependencies in Cargo.toml:
[dependencies]
tokio = { version = "1.17.0", features = ["macros", "rt-multi-thread"] }
rspotify = { version = "0.11.5", features = ["cli", "env-file"] }
env_logger = "0.9.0"
  • Create a file .env in the project directory with client ID and client secret from the application in Spotify Dev Console and a redirect URL:
RSPOTIFY_CLIENT_ID=[redacted]
RSPOTIFY_CLIENT_SECRET=[redacted]
RSPOTIFY_REDIRECT_URI=https://example.com/
  • Copy examples/with_refresh_token.rs into main.rs.
  • Run it
  • A browser window opens, log in and confirm access if necessary
  • As instructed by the program, copy the example.com URL from the browser's address bar to the console where your program is running and press Enter

Actual behavior:

[...]
Followed 3 artists successfully.
User currently follows at least 3 artists.
Unfollowed 3 artists successfully.
>>> Session two, running some requests:
thread 'main' panicked at 'Rspotify not authenticated', c:\dev-tools\Rust\.cargo\registry\src\github.com-1ecc6299db9ec823\rspotify-0.11.5\src\clients\base.rs:100:14
[...]

Expected behavior
Something like:

[...]
Followed 3 artists successfully.
User currently follows at least 3 artists.
Unfollowed 3 artists successfully.
>>> Session two, running some requests:
Followed 3 artists successfully.
User currently follows at least 3 artists.
Unfollowed 3 artists successfully.
[...]
Originally created by @AndreKR on GitHub (Apr 12, 2022). Original GitHub issue: https://github.com/ramsayleung/rspotify/issues/310 **Describe the bug** The supplied [`examples/with_refresh_token.rs`](https://github.com/ramsayleung/rspotify/blob/0b4f3aa1058b961f307b7b6798b3cb875b30d24e/examples/with_refresh_token.rs) exits with an error. **To Reproduce** * Create a new Rust project * Set up dependencies in `Cargo.toml`: ```toml [dependencies] tokio = { version = "1.17.0", features = ["macros", "rt-multi-thread"] } rspotify = { version = "0.11.5", features = ["cli", "env-file"] } env_logger = "0.9.0" ``` * Create a file `.env` in the project directory with client ID and client secret from the application in Spotify Dev Console and a redirect URL: ```shell RSPOTIFY_CLIENT_ID=[redacted] RSPOTIFY_CLIENT_SECRET=[redacted] RSPOTIFY_REDIRECT_URI=https://example.com/ ``` * Copy [`examples/with_refresh_token.rs`](https://github.com/ramsayleung/rspotify/blob/0b4f3aa1058b961f307b7b6798b3cb875b30d24e/examples/with_refresh_token.rs) into `main.rs`. * Run it * A browser window opens, log in and confirm access if necessary * As instructed by the program, copy the `example.com` URL from the browser's address bar to the console where your program is running and press Enter **Actual behavior:** ```plain [...] Followed 3 artists successfully. User currently follows at least 3 artists. Unfollowed 3 artists successfully. >>> Session two, running some requests: thread 'main' panicked at 'Rspotify not authenticated', c:\dev-tools\Rust\.cargo\registry\src\github.com-1ecc6299db9ec823\rspotify-0.11.5\src\clients\base.rs:100:14 [...] ``` **Expected behavior** Something like: ```plain [...] Followed 3 artists successfully. User currently follows at least 3 artists. Unfollowed 3 artists successfully. >>> Session two, running some requests: Followed 3 artists successfully. User currently follows at least 3 artists. Unfollowed 3 artists successfully. [...] ```
kerem 2026-02-27 20:23:05 +03:00
Author
Owner

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

Sorry for not replying earlier, I was too busy at work, and now I am finally have time to check this issue since I am on vocation.

I can't reproduce this error, I run the refresh token example like this:

╭─ramsayliang@VM-237-156-centos ~/code/rust/rspotify ‹master›
╰─$ cargo run --example with_auto_reauth --features env-file,cli,client-reqwest,reqwest-default-tls                                             101 ↵
    Finished dev [unoptimized + debuginfo] target(s) in 0.10s
     Running `target/debug/examples/with_auto_reauth`
>>> Session one, obtaining refresh token and running some requests:
Opened https://accounts.spotify.com/authorize?scope=user-follow-read+user-follow-modify&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A8000%
2Fcallback&client_id={my-client-id}&state=SoYLadWdS7WcHPHB in your browser.
Please enter the URL you were redirected to:
http://localhost:8000/callback?code={response-code-balabala}&state=SoYLadWdS7WcHPHB
Followed 3 artists successfully.
User currently follows at least 5 artists.
Unfollowed 3 artists successfully.
>>> Session two, the token should expire, then re-auth automatically
Followed 3 artists successfully.
User currently follows at least 5 artists.
Unfollowed 3 artists successfully.
>>> New Session one from ClientCredsSpotify, obtaining token and doing things
Get albums: spotify:album:0sNOF9WDwhWunNAHPD3Baj
>>> New Session two from ClientCredsSpotify, expiring the token and then re-auth automatically
Get albums: spotify:album:0sNOF9WDwhWunNAHPD3Baj
thread 'main' panicked at 'Rspotify not authenticated', c:\dev-tools\Rust\.cargo\registry\src\github.com-1ecc6299db9ec823\rspotify-0.11.5\src\clients\base.rs:100:14

The error message complains that 'Rspotify is not authenticated', I guess there is something wrong with your authentication process.

What's the output if you run cargo run --example auth_code --features env-file,cli,client-reqwest in Rspotify project?

What about cargo run --example with_auto_reauth --features env-file,cli,client-reqwest,reqwest-default-tls?

<!-- gh-comment-id:1114092508 --> @ramsayleung commented on GitHub (May 1, 2022): Sorry for not replying earlier, I was too busy at work, and now I am finally have time to check this issue since I am on vocation. I can't reproduce this error, I run the refresh token example like this: ```sh ╭─ramsayliang@VM-237-156-centos ~/code/rust/rspotify ‹master› ╰─$ cargo run --example with_auto_reauth --features env-file,cli,client-reqwest,reqwest-default-tls 101 ↵ Finished dev [unoptimized + debuginfo] target(s) in 0.10s Running `target/debug/examples/with_auto_reauth` >>> Session one, obtaining refresh token and running some requests: Opened https://accounts.spotify.com/authorize?scope=user-follow-read+user-follow-modify&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A8000% 2Fcallback&client_id={my-client-id}&state=SoYLadWdS7WcHPHB in your browser. Please enter the URL you were redirected to: http://localhost:8000/callback?code={response-code-balabala}&state=SoYLadWdS7WcHPHB Followed 3 artists successfully. User currently follows at least 5 artists. Unfollowed 3 artists successfully. >>> Session two, the token should expire, then re-auth automatically Followed 3 artists successfully. User currently follows at least 5 artists. Unfollowed 3 artists successfully. >>> New Session one from ClientCredsSpotify, obtaining token and doing things Get albums: spotify:album:0sNOF9WDwhWunNAHPD3Baj >>> New Session two from ClientCredsSpotify, expiring the token and then re-auth automatically Get albums: spotify:album:0sNOF9WDwhWunNAHPD3Baj ``` ```sh thread 'main' panicked at 'Rspotify not authenticated', c:\dev-tools\Rust\.cargo\registry\src\github.com-1ecc6299db9ec823\rspotify-0.11.5\src\clients\base.rs:100:14 ``` The error message complains that 'Rspotify is not authenticated', I guess there is something wrong with your authentication process. What's the output if you run `cargo run --example auth_code --features env-file,cli,client-reqwest` in Rspotify project? What about `cargo run --example with_auto_reauth --features env-file,cli,client-reqwest,reqwest-default-tls`?
Author
Owner

@AndreKR commented on GitHub (May 1, 2022):

In your log output you're running the with_auto_reauth example, I was talking about the with_refresh_token example.

Both the auth_code and with_auto_reauth examples work for me as well, it's only with_refresh_token that doesn't work.

<!-- gh-comment-id:1114108149 --> @AndreKR commented on GitHub (May 1, 2022): In your log output you're running the `with_auto_reauth` example, I was talking about the `with_refresh_token` example. Both the `auth_code` and `with_auto_reauth` examples work for me as well, it's only `with_refresh_token` that doesn't work.
Author
Owner

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

Oooh, my bad, I can reproduce this issue, I am trying to figure out what's going on.

Followed 3 artists successfully.
User currently follows at least 5 artists.
Unfollowed 3 artists successfully.
>>> Session two, running some requests:
thread 'main' panicked at 'Rspotify not authenticated', /data/mm64/ramsayliang/code/rust/rspotify/src/clients/base.rs:100:14
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

PS:
Thanks for your report, I have addressed the root cause of this problem, and created a PR to fix it.

<!-- gh-comment-id:1114119890 --> @ramsayleung commented on GitHub (May 1, 2022): Oooh, my bad, I can reproduce this issue, I am trying to figure out what's going on. ```sh Followed 3 artists successfully. User currently follows at least 5 artists. Unfollowed 3 artists successfully. >>> Session two, running some requests: thread 'main' panicked at 'Rspotify not authenticated', /data/mm64/ramsayliang/code/rust/rspotify/src/clients/base.rs:100:14 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ``` PS: Thanks for your report, I have addressed the root cause of this problem, and created a PR to fix it.
Author
Owner

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

@AndreKR This PR should fix this example, since Mario are too busy to review this PR, if you can't wait for this PR merged, you could take a peek into the modification of this PR.

<!-- gh-comment-id:1124484939 --> @ramsayleung commented on GitHub (May 12, 2022): @AndreKR This PR should fix this example, since Mario are too busy to review this PR, if you can't wait for this PR merged, you could take a peek into the modification of this PR.
Author
Owner

@marioortizmanero commented on GitHub (May 16, 2022):

Hi! Sorry for not being too active lately. I was fighting my finals and didn't have much time. Just merged the PR :)

<!-- gh-comment-id:1128200184 --> @marioortizmanero commented on GitHub (May 16, 2022): Hi! Sorry for not being too active lately. I was fighting my finals and didn't have much time. Just merged the PR :)
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#95
No description provided.