spotify web api 授权码授权 thelinmichael/spotify-web-api-java android
Posted
技术标签:
【中文标题】spotify web api 授权码授权 thelinmichael/spotify-web-api-java android【英文标题】:spotify web api authorization code grant thelinmichael/spotify-web-api-java android 【发布时间】:2018-05-21 21:08:37 【问题描述】:我在使用授权码授予授权 spotify web api 时遇到问题。我知道我必须填写我的客户端 ID、客户端密码并将 uri 重定向到字符串,但我不知道如何获取名为 code 的字符串,这是获取访问令牌所必需的。
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. */
);
你知道如何获取此代码并成功获取访问令牌吗?
谢谢!
【问题讨论】:
【参考方案1】:正如您在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。这就是它变得复杂的地方。您需要接收 Spotify 在回调 url 中提供的代码参数。此代码参数是正在进行的过程所需的值。您正在使用的 spotify-web-api-java 不提供任何东西来接收此请求。您需要使用例如找到解决方案Spring RESTful Web Service。
【讨论】:
【参考方案2】:我现在已经解决了这个问题。
您不必创建 Web 服务! 解决方案实际上非常简单。您只需要使用
创建一个 IntentIntent.ACTION_VIEW
作为第一个参数,并以您的 url 作为第二个参数,并以 Intent 作为参数调用 startActivity。 然后你必须添加这个
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="callback"
android:scheme="yourcustomprotocol" />
</intent-filter>
到清单中的活动标记,最后但并非最不重要的是在
中捕获回调onNewIntent(最终意图)
在这里,您可以获取可以从中提取代码的意图的 url。
【讨论】:
以上是关于spotify web api 授权码授权 thelinmichael/spotify-web-api-java android的主要内容,如果未能解决你的问题,请参考以下文章
Spotify API 从 redirect_uri 获取授权码