gpt4 book ai didi

java - Spotify api 获取访问 token /代码

转载 作者:太空宇宙 更新时间:2023-11-04 11:42:17 25 4
gpt4 key购买 nike

所以我尝试使用这个库 library 访问我的Spotify帐户,但我不知道如何获得访问 token ,但我不知道如何从授权URL获取响应我已经创建了一个访问URL并打印出响应的输入流,但我没有给出正确的输出,我还创建了一个关闭接收响应的服务器,但我什么也没得到,我从来没有使用过Java服务器/网络,所以我可能犯了一个错误......

public class privat {
public privat() throws IOException {


final String clientId = "clientId ";
final String clientSecret = "clientSecret code ";
final String redirectUri = "http://localhost:8888/callback";
final Api api = Api.builder()
.clientId(clientId)
.clientSecret(clientSecret)
.redirectURI(redirectUri)
.build();

/* Set the necessary scopes that the application will need from the user */
final List<String> scopes = Arrays.asList("user-read-private", "user-read-email");

/* Set a state. This is used to prevent cross site request forgeries. */
final String state = "someExpectedStateString";

String authorizeURL = api.createAuthorizeURL(scopes, state);
System.out.println(authorizeURL);

/* Continue by sending the user to the authorizeURL, which will look something like
https://accounts.spotify.com:443/authorize?client_id=5fe01282e44241328a84e7c5cc169165&response_type=code&redirect_uri=https://example.com/callback&scope=user-read-private%20user-read-email&state=some-state-of-my-choice
*/


/* Application details necessary to get an access token */
final String code = "" ;/* where to find this ?? */



/* Make a token request. Asynchronous requests are made with the .getAsync method and synchronous requests
* are made with the .get method. This holds for all type of requests. */
final SettableFuture<AuthorizationCodeCredentials> authorizationCodeCredentialsFuture = api.authorizationCodeGrant(code).build().getAsync();

/* Add callbacks to handle success and failure */
Futures.addCallback(authorizationCodeCredentialsFuture, new FutureCallback<AuthorizationCodeCredentials>() {
@Override
public void onSuccess(AuthorizationCodeCredentials authorizationCodeCredentials) {
/* The tokens were retrieved successfully! */
System.out.println("Successfully retrieved an access token! " + authorizationCodeCredentials.getAccessToken());
System.out.println("The access token expires in " + authorizationCodeCredentials.getExpiresIn() + " seconds");
System.out.println("Luckily, I can refresh it using this refresh token! " + authorizationCodeCredentials.getRefreshToken());

/* Set the access token and refresh token so that they are used whenever needed */
api.setAccessToken(authorizationCodeCredentials.getAccessToken());
api.setRefreshToken(authorizationCodeCredentials.getRefreshToken());
}

@Override
public void onFailure(Throwable throwable) {
/* Let's say that the client id is invalid, or the code has been used more than once,
* the request will fail. Why it fails is written in the throwable's message. */
System.out.println(throwable.getMessage());
System.out.println(throwable.getStackTrace());
}
});
}

}

最佳答案

一旦用户授权您的应用程序,代码就会作为回调 URL 的查询参数。您需要找到一种方法从那里获取它 - 您可以在 localhost:8888 上启动 Web 服务器以从那里获取代码 - 或者您可以指示用户在重定向后从重定向 URI 的查询参数中复制代码。您可以找到有关授权过程的更多信息(看起来授权代码隐式授权流程都适合您)on the Spotify Developer site .

关于java - Spotify api 获取访问 token /代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42673515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com