-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthorizationCodeExample.java
More file actions
65 lines (54 loc) · 2.44 KB
/
Copy pathAuthorizationCodeExample.java
File metadata and controls
65 lines (54 loc) · 2.44 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
package examples;
import de.sonallux.spotify.api.SpotifyApiException;
import de.sonallux.spotify.api.SpotifyWebApi;
import de.sonallux.spotify.api.authorization.InMemoryTokenStore;
import de.sonallux.spotify.api.authorization.Scope;
import de.sonallux.spotify.api.authorization.SpotifyAuthorizationException;
import de.sonallux.spotify.api.authorization.authorization_code.AuthorizationCodeFlow;
public class AuthorizationCodeExample {
private final SpotifyWebApi spotifyApi;
private final AuthorizationCodeFlow authCodeFlow;
public AuthorizationCodeExample() {
var tokenStore = new InMemoryTokenStore();
this.authCodeFlow = new AuthorizationCodeFlow("<client id>", "<client secret>", "<redirect uri>", tokenStore);
this.spotifyApi = SpotifyWebApi.builder().authorization(authCodeFlow).build();
}
public static void main(String[] args) {
var example = new AuthorizationCodeExample();
if (example.authorize()) {
// Once authorized the access token will be automatically renewed, if is has expired
example.useTheSpotifyWebApi();
}
}
private boolean authorize() {
// Navigate the user to this authorizationUrl, so he can grant access
var authorizationUrl = authCodeFlow.createAuthorizationUrl()
.state("<state>")
.scopes(Scope.USER_LIBRARY_READ)
//.showDialog(true)
.build();
// After successful authorization the user will be redirected
var responseUrl = "<redirect response url>";
var redirectResponse = authCodeFlow.parseAuthorizationRedirectResponse(responseUrl);
//Validate redirectResponse.getState() with the state parameter you choose for the authorization url
if (!redirectResponse.isSuccess()) {
System.err.println("Authorization failed: " + redirectResponse.getError());
return false;
}
try {
authCodeFlow.exchangeAuthorizationCode(redirectResponse);
return true;
} catch (SpotifyAuthorizationException e) {
e.printStackTrace();
return false;
}
}
private void useTheSpotifyWebApi() {
try {
var usersSavedTracks = spotifyApi.getTracksApi().getUsersSavedTracks().build().execute();
usersSavedTracks.getItems().forEach(System.out::println);
} catch (SpotifyApiException e) {
e.printStackTrace();
}
}
}