【问题标题】:bad token + no refresh token on spotify api错误令牌 + Spotify api 上没有刷新令牌
【发布时间】:2022-07-15 06:54:40
【问题描述】:

我正在使用 spotify API 制作应用程序,当我尝试获取访问令牌和刷新令牌时出现问题。在 json 响应中,我没有任何刷新令牌,并且给定的访问令牌不起作用(与我直接在 spotify 网站上获得的令牌相比太短了。 如果您发现有问题请告诉我(spotify abi 基于 Oauth2.0)

这是我的代码


        try {
            String urlString = "https://accounts.spotify.com/api/token?";

            URL website = new URL(urlString);

            HttpURLConnection connection = (HttpURLConnection) website.openConnection();
            connection.setRequestMethod("POST");


            // Headers
            connection.setRequestProperty("Accept", "application/json");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            connection.setDoOutput(true);

            // Add parameters to the body
            HashMap<String, String> params = new HashMap<>();
            params.put("grant_type", "client_credentials");
            params.put("redirect_uri", ID.REDIRECT_URI);
            params.put("code", code);
            params.put("client_id", ID.CLIENT_ID);
            params.put("client_secret", ID.CLIEN_SECRET_ID);

            OutputStream os = connection.getOutputStream();
            BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, StandardCharsets.UTF_8));
            writer.write(getPostDataString(params));
            writer.flush();
            writer.close();
            os.close();

            // Open the connection
            connection.connect();

            JsonObject jsonResponse  = Http.statusResponse(connection);

            // Close the connection
            connection.disconnect();

            System.out.println(jsonResponse);
            return jsonResponse;


        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

这是我得到的:

{"access_token":"BQAQxzMFIqOY2vk9aWintAgOilaY77N6s-xL2nyHmVzWMsnu4t3wmvGJ-EK_2MDMXvniBEeYoydvbYZpxOY","token_type":"Bearer","expires_in":3600}

这是我应该得到的(基于 spotify 指南:https://developer.spotify.com/documentation/general/guides/authorization/code-flow/

{ "access_token": "NgCXRK...MzYjw", "token_type": "承载者", "scope": "user-read-private user-read-email", “expires_in”:3600, "refresh_token": "NgAagA...Um_SHo" }

【问题讨论】:

    标签: api token access-token spotify refresh-token


    【解决方案1】:

    您的grant_type 参数应为authorization_code 而不是client_credentialsclient_credentials 授权类型不授予对授权范围的访问权限。

    所以使用params.put("grant_type", "authorization_code");

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2021-10-11
      • 1970-01-01
      • 2021-07-19
      • 1970-01-01
      • 2017-06-26
      • 2014-10-25
      • 1970-01-01
      • 2023-02-02
      • 1970-01-01
      相关资源
      最近更新 更多