[GH-ISSUE #367] Enable loading refresh/access token from env #112

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

Originally created by @djedlajn on GitHub (Oct 27, 2022).
Original GitHub issue: https://github.com/ramsayleung/rspotify/issues/367

Is your feature request related to a problem? Please describe.

I would appreciate the ability to load the access/refresh token pair from ENV and use them. Maybe it's possible now but if not I'm willing to put some effort into making such PR (mind I'm very new to Rust so it might take a while).

Describe the solution you'd like
Load the access/refresh token manually to avoid the need for prompting for auth.

Originally created by @djedlajn on GitHub (Oct 27, 2022). Original GitHub issue: https://github.com/ramsayleung/rspotify/issues/367 **Is your feature request related to a problem? Please describe.** I would appreciate the ability to load the access/refresh token pair from ENV and use them. Maybe it's possible now but if not I'm willing to put some effort into making such PR (mind I'm very new to Rust so it might take a while). **Describe the solution you'd like** Load the access/refresh token manually to avoid the need for prompting for auth.
kerem 2026-02-27 20:23:11 +03:00
Author
Owner

@ramsayleung commented on GitHub (Oct 29, 2022):

I would appreciate the ability to load the access/refresh token pair from ENV and use them.

You could directly read access_token and refresh_token from the environment variable, and assign them to the Token. Taking the test_read_token_cache function for example, instead of hard coding the token, you could read the token from ENV:

async fn read_token_from_env() {
    let expires_in = Duration::seconds(3600);
    let expires_at = Some(Utc::now() + expires_in);
    let scopes = scopes!("playlist-read-private", "playlist-read-collaborative");

    let tok = Token {
        expires_in,
        expires_at,
        access_token: env::var("RSPOTIFY_ACCESS_TOKEN").ok()?, // read access token from ENV
        scopes: scopes.clone(),
        refresh_token: env::var("RSPOTIFY_REFRESH_TOKEN").ok(), // read refresh token from ENV
    };

    let config = Config {
        token_cached: true,
        cache_path: PathBuf::from(".test_read_token_cache.json"),
        ..Default::default()
    };
    let mut predefined_spotify = ClientCredsSpotify::from_token(tok);
    predefined_spotify.config = config.clone();
}
<!-- gh-comment-id:1295685472 --> @ramsayleung commented on GitHub (Oct 29, 2022): > I would appreciate the ability to load the access/refresh token pair from ENV and use them. You could directly read `access_token` and `refresh_token` from the environment variable, and assign them to the `Token`. Taking the [`test_read_token_cache`](https://github.com/ramsayleung/rspotify/blob/aedba67498b4abfd67a10be111d6f2a2dc9705c9/tests/test_oauth2.rs#L36) function for example, instead of hard coding the token, you could read the token from ENV: ```rust async fn read_token_from_env() { let expires_in = Duration::seconds(3600); let expires_at = Some(Utc::now() + expires_in); let scopes = scopes!("playlist-read-private", "playlist-read-collaborative"); let tok = Token { expires_in, expires_at, access_token: env::var("RSPOTIFY_ACCESS_TOKEN").ok()?, // read access token from ENV scopes: scopes.clone(), refresh_token: env::var("RSPOTIFY_REFRESH_TOKEN").ok(), // read refresh token from ENV }; let config = Config { token_cached: true, cache_path: PathBuf::from(".test_read_token_cache.json"), ..Default::default() }; let mut predefined_spotify = ClientCredsSpotify::from_token(tok); predefined_spotify.config = config.clone(); } ```
Author
Owner

@djedlajn commented on GitHub (Oct 30, 2022):

@ramsayleung That works perfectly. Thanks for the hints.

<!-- gh-comment-id:1296343291 --> @djedlajn commented on GitHub (Oct 30, 2022): @ramsayleung That works perfectly. Thanks for the hints.
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#112
No description provided.