-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathoauth_tokens.rs
53 lines (46 loc) · 1.78 KB
/
oauth_tokens.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! This example is specially useful for the OAuth tests. It simply obtains an
//! access token and a refresh token with all available scopes.
//!
//! Set RSPOTIFY_CLIENT_ID, RSPOTIFY_CLIENT_SECRET and RSPOTIFY_REDIRECT_URI in
//! an .env file or export them manually as environmental variables for this to
//! work.
use rspotify::{prelude::*, scopes, AuthCodeSpotify, Credentials, OAuth};
#[tokio::main]
async fn main() {
// You can use any logger for debugging.
env_logger::init();
// The credentials must be available in the environment. Enable the
// `env-file` feature in order to read them from an `.env` file.
let creds = Credentials::from_env().unwrap();
// Using every possible scope
let scopes = scopes!(
"user-read-email",
"user-read-private",
"user-top-read",
"user-read-recently-played",
"user-follow-read",
"user-library-read",
"user-read-currently-playing",
"user-read-playback-state",
"user-read-playback-position",
"playlist-read-collaborative",
"playlist-read-private",
"user-follow-modify",
"user-library-modify",
"user-modify-playback-state",
"playlist-modify-public",
"playlist-modify-private",
"ugc-image-upload"
);
let oauth = OAuth::from_env(scopes).unwrap();
let spotify = AuthCodeSpotify::new(creds, oauth);
let url = spotify.get_authorize_url(false).unwrap();
// This function requires the `cli` feature enabled.
spotify.prompt_for_token(&url).await.unwrap();
let token = spotify.token.lock().await.unwrap();
println!("Access token: {}", &token.as_ref().unwrap().access_token);
println!(
"Refresh token: {}",
token.as_ref().unwrap().refresh_token.as_ref().unwrap()
);
}