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

归志宁无五亩园,读书本意在元元。这篇文章主要讲述spotify web api授权码授予thelinmichael / spotify-web-api-java android相关的知识,希望能为你提供帮助。
我有问题使用授权代码授权授权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. */} });

您知道如何获取此代码并成功获取访问令牌吗?
谢谢!
答案您可以在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提供的代码参数。此代码参数是正在进行的过程所需的值。您正在使用的spotify-web-api-java不提供接收此请求的任何内容。你需要通过使用例如一个Spring RESTful Web Service。
另一答案我现在已经解决了这个问题。
您不必制作Web服务!解决方案实际上非常简单。你只需要创建一个Intent
Intent.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>

【spotify web api授权码授予thelinmichael / spotify-web-api-java android】清单中的活动标记,最后但并非最不重要的是捕获回调
onNewIntent(最终意图)
在这里你可以获得意图的url,你可以从中提取代码。

    推荐阅读