我正在尝试学习spotify api。我已经从https://developer.spotify.com/创建了一个应用,并获取了 客户ID 和 客户端密钥
现在我要做的是获取我的个人资料的属性。我查看了此处https://github.com/thelinmichael/spotify-web-api-java的代码示例,并输入了下面显示的代码
public static void main(String[] args) {
final String clientId = "My Client ID";
final String clientSecret = "My Client Secret";
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");
/* Set access token on the Api object so that it's used going forward */
api.setAccessToken(clientCredentials.getAccessToken());
/* 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. */
throwable.printStackTrace();
}
});
final CurrentUserRequest currentUserRequest = api.getMe().build();
try {
final User user = currentUserRequest.get();
System.out.println("Display name: " + user.getDisplayName());
} catch (Exception e) {
System.out.println("Something went wrong! " + e.getMessage());
e.printStackTrace();
}
}
我正在成功检索访问令牌,但当我尝试获取当前用户时出现问题。这是代码的输出:
Successfully retrieved an access token! [my access token]
The access token expires in 3600 seconds
Something went wrong! 401
com.wrapper.spotify.exceptions.BadRequestException: 401
at com.wrapper.spotify.SpotifyHttpManager.handleErrorStatusCode(SpotifyHttpManager.java:178)
at com.wrapper.spotify.SpotifyHttpManager.execute(SpotifyHttpManager.java:157)
at com.wrapper.spotify.SpotifyHttpManager.get(SpotifyHttpManager.java:53)
at com.wrapper.spotify.methods.AbstractRequest.getJson(AbstractRequest.java:34)
at com.wrapper.spotify.methods.CurrentUserRequest.get(CurrentUserRequest.java:32)
at com.kadir.spotify.Main.main(Main.java:52)
我在这里缺少什么?提前谢谢。