我在使用授权代码授予来授权Spotify Web API时遇到问题。我知道我必须填写我的客户ID,客户机密并将uri重定向到字符串,但是我不知道如何获取称为代码的字符串,这是获取访问令牌所必需的。

final String clientId = "<your_client_id>";
final String clientSecret = "<your_client_secret>";
final String redirectURI = "<your_redirect_uri>";

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);

/* 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 = "<insert code>"; //I don't know where I get the value for this string from

/* 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! */

    /* 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. */

      }
});


您知道如何获取此代码并成功获取访问令牌吗?

谢谢!

最佳答案

如您在Authorization Code Flow的说明中所读,您需要将用户发送到Spotify URL。该URL在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
*/


用户登录并获得访问给定范围的“应用程序”权限后,Spotify会将用户重定向到回调URL。这就是它变得复杂的地方。您需要在回调URL中接收Spotify提供的代码参数。此代码参数是正在进行的过程所需的值。您正在使用的spotify-web-api-java没有提供任何内容来接收此请求。您需要使用以下方法找到解决方案: Spring RESTful Web Service

关于java - Spotify Web API授权代码授予thelinmichael/spotify-web-api-java android,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47700492/

10-11 22:25
查看更多