java - spotify web api授权代码授予thelinmichael/spotify-web-api-java android

标签 java android authorization spotify

我在使用授权码授予授权 spotify web api 时遇到问题。我知道我必须填写我的客户端 ID、客户端密码并将 uri 重定向到字符串,但我不知道如何获取名为 code 的字符串,这是获取访问 token 所必需的。

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

      }
});

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

谢谢!

最佳答案

正如您在 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/

相关文章:

java - Android Studio DatePicker/TimePicker 渲染/空指针异常问题?

java - UI-Update 后 SpringLayout 中组件的宽度等于 0

java - 连接两个 AWS EC2 实例以将数据从一个 EC2 实例共享到另一 EC2 实例

mongodb - 使用管理数据库中设置的正确凭据登录失败 - MongoDb

php - 在 PHP 中添加用户身份验证

c# - .net core [授权] 将 ClaimsIdentity 与 AAD 组结合使用

java - 播放压缩文件中的音频文件

android - popBackStack 之后错误的 day_of_month

Android Studio imageButton 和切换到另一个 Activity

android - Android studio 中模拟器运行的WebView 无法上网,而Chrome 可以?