Skip to content

Commit 0e5531f

Browse files
docs: Document examples (#1567)
Document the process for running the examples. This was fairly non-obvious and I spent a fair bit of time trying and failing to get a client credentials token to work before finding in some discussion that this is known not to work. The wiki mentions using the oauth example as a way to get a token and I think that is likely to be the best way to get a token for the purposes of running the examples too.
1 parent f16a30e commit 0e5531f

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

examples/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Examples
2+
3+
This folder contains examples of how to use the `librespot` library for various purposes.
4+
5+
## How to run the examples
6+
7+
In general, to invoke an example, clone down the repo and use `cargo` as follows:
8+
9+
```
10+
cargo run --example [filename]
11+
```
12+
13+
in which `filename` is the file name of the example, for instance `get_token` or `play`.
14+
15+
### Acquiring an access token
16+
17+
Most examples require an access token as the first positional argument. **Note that an access token
18+
gained by the client credentials flow will not work**. `librespot-oauth` provides a utility to
19+
acquire an access token using an OAuth flow, which will be able to run the examples. To invoke this,
20+
run:
21+
22+
```
23+
cargo run --package librespot-oauth --example oauth_sync
24+
```
25+
26+
A browser window will open and prompt you to authorize with Spotify. Once done, take the
27+
`access_token` property from the dumped object response and proceed to use it in examples. You may
28+
find it convenient to save it in a shell variable like `$ACCESS_TOKEN`.
29+
30+
Once you have obtained the token you can proceed to run the example. Check each individual
31+
file to see what arguments are expected. As a demonstration, here is how to invoke the `play`
32+
example to play a song -- the second argument is the URI of the track to play.
33+
34+
```
35+
cargo run --example play "$ACCESS_TOKEN" 2WUy2Uywcj5cP0IXQagO3z
36+
```

0 commit comments

Comments
 (0)