-
Notifications
You must be signed in to change notification settings - Fork 123
/
auth_code.rs
53 lines (46 loc) · 1.63 KB
/
auth_code.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
use rspotify::{
model::{AdditionalType, Country, Market},
prelude::*,
scopes, AuthCodeSpotify, Credentials, OAuth,
};
#[tokio::main]
async fn main() {
// You can use any logger for debugging.
env_logger::init();
// Set RSPOTIFY_CLIENT_ID and RSPOTIFY_CLIENT_SECRET in an .env file (after
// enabling the `env-file` feature) or export them manually:
//
// export RSPOTIFY_CLIENT_ID="your client_id"
// export RSPOTIFY_CLIENT_SECRET="secret"
//
// These will then be read with `from_env`.
//
// Otherwise, set client_id and client_secret explictly:
//
// ```
// let creds = Credentials::new("my-client-id", "my-client-secret");
// ```
let creds = Credentials::from_env().unwrap();
// Same for RSPOTIFY_REDIRECT_URI. You can also set it explictly:
//
// ```
// let oauth = OAuth {
// redirect_uri: "http://localhost:8888/callback".to_string(),
// scopes: scopes!("user-read-recently-played"),
// ..Default::default(),
// };
// ```
let oauth = OAuth::from_env(scopes!("user-read-currently-playing")).unwrap();
let spotify = AuthCodeSpotify::new(creds, oauth);
// Obtaining the access token
let url = spotify.get_authorize_url(false).unwrap();
// This function requires the `cli` feature enabled.
spotify.prompt_for_token(&url).await.unwrap();
// Running the requests
let market = Market::Country(Country::Spain);
let additional_types = [AdditionalType::Episode];
let artists = spotify
.current_playing(Some(market), Some(&additional_types))
.await;
println!("Response: {artists:?}");
}