forked from spotify-web-api-java/spotify-web-api-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientCredentialsFlow.java
More file actions
57 lines (46 loc) · 2.24 KB
/
ClientCredentialsFlow.java
File metadata and controls
57 lines (46 loc) · 2.24 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
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.SettableFuture;
import com.wrapper.spotify.Api;
import com.wrapper.spotify.methods.authentication.ClientCredentialsGrantRequest;
import com.wrapper.spotify.models.ClientCredentials;
import static junit.framework.TestCase.fail;
/**
* This example shows how to get refresh an access token asynchronously. There's a
* synchronous version of the method available as well.
*
* The authorization flow used is documented in detail at
* https://developer.spotify.com/spotify-web-api/authorization-guide/#client-credentials-flow
* in the "Client Credentials" section.
*/
public class ApplicationAuthentication {
public static void main(String[] strings) {
final String clientId = "<insert client id>";
final String clientSecret = "<insert client secret>";
final Api api = Api.builder()
.clientId(clientId)
.clientSecret(clientSecret)
.build();
/* Create a request object. */
final ClientCredentialsGrantRequest request = api.clientCredentialsGrant().build();
/* Use the request object to make the request, either asynchronously (getAsync) or synchronously (get) */
final SettableFuture<ClientCredentials> responseFuture = request.getAsync();
/* Add callbacks to handle success and failure */
Futures.addCallback(responseFuture, new FutureCallback<ClientCredentials>() {
@Override
public void onSuccess(ClientCredentials clientCredentials) {
/* The tokens were retrieved successfully! */
System.out.println("Successfully retrieved an access token! " + clientCredentials.getAccessToken());
System.out.println("The access token expires in " + clientCredentials.getExpiresIn() + " seconds");
/* Please note that this flow does not return a refresh token.
* That's only for the Authorization code flow */
}
@Override
public void onFailure(Throwable throwable) {
/* An error occurred while getting the access token. This is probably caused by the client id or
* client secret is invalid. */
fail("Failed to resolve future: " + throwable.getMessage());
}
});
}
}