【问题标题】:Spotify api getting acces token / codeSpotify api获取访问令牌/代码
【发布时间】:2017-03-08 14:07:51
【问题描述】:

所以我正在尝试使用这个库library 访问我的 Spotify 帐户,但我不知道如何获得访问令牌,但我不知道如何从授权 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());
            }
        });
    }

}

【问题讨论】:

    标签: java spotify


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-01-06
      • 2017-02-14
      • 2018-01-08
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 2020-01-09
      • 2017-06-26
      • 2020-05-05
      相关资源
      最近更新 更多