forked from ramsayleung/rspotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_playback.rs
52 lines (48 loc) · 2.02 KB
/
start_playback.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
extern crate rspotify;
use rspotify::client::Spotify;
use rspotify::model::offset::for_position;
use rspotify::oauth2::{SpotifyClientCredentials, SpotifyOAuth};
use rspotify::util::get_token;
#[tokio::main]
async fn main() {
// Set client_id and client_secret in .env file or
// export CLIENT_ID="your client_id"
// export CLIENT_SECRET="secret"
// export REDIRECT_URI=your-direct-uri
// Or set client_id, client_secret,redirect_uri explictly
// let oauth = SpotifyOAuth::default()
// .client_id("this-is-my-client-id")
// .client_secret("this-is-my-client-secret")
// .redirect_uri("http://localhost:8888/callback")
// .build();
let mut oauth = SpotifyOAuth::default()
.scope("user-modify-playback-state")
.build();
match get_token(&mut oauth).await {
Some(token_info) => {
let client_credential = SpotifyClientCredentials::default()
.token_info(token_info)
.build();
// Or set client_id and client_secret explictly
// let client_credential = SpotifyClientCredentials::default()
// .client_id("this-is-my-client-id")
// .client_secret("this-is-my-client-secret")
// .build();
let spotify = Spotify::default()
.client_credentials_manager(client_credential)
.build();
// this is the example device_id from spotify website,
// so it will raise a 403 error, just change this device_id to yours
let device_id = String::from("74ASZWbe4lXaubB36ztrGX");
let uris = vec!["spotify:track:4iV5W9uYEdYUVa79Axb7Rh".to_owned()];
match spotify
.start_playback(Some(device_id), None, Some(uris), for_position(0), None)
.await
{
Ok(_) => println!("start playback successful"),
Err(_) => eprintln!("start playback failed"),
}
}
None => println!("auth failed"),
};
}