【问题标题】:Java HttpsURLConnection - Authorization with Spotify APIJava HttpsURLConnection - 使用 Spotify API 进行授权
【发布时间】:2021-08-14 17:29:18
【问题描述】:

这几天我一直在努力让它工作。 我正在尝试使用 Java 的 Client Credentials 授权流程向 Spotify 授权。但是,尽管有 Content-Length 属性,但我仍然收到 411 Length Required HTTP 错误。

HttpsURLConnection spotifyAccounts = null;
            try {
                spotifyAccounts = (HttpsURLConnection) new URL("https://accounts.spotify.com/api/token?grant_type=client_credentials").openConnection();
                spotifyAccounts.setDoOutput(true);
                spotifyAccounts.setDoInput(true);
                spotifyAccounts.setRequestMethod("POST");
                String unencodedAuthorizationKey = "Basic " + "42f48d16044a4c79b5168c5784889827" + ":" + "d6bb7a72e99342319e6b6d8c6fd3cedd";
                String encodedKey = Base64.getEncoder().encodeToString(unencodedAuthorizationKey.getBytes());
                spotifyAccounts.setRequestProperty("Authorization", encodedKey);
                spotifyAccounts.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                spotifyAccounts.setRequestProperty("User-Agent", "Mozilla/5.0");
                int totalLength = 0;
                for(Entry<String, List<String>> entry : spotifyAccounts.getRequestProperties().entrySet()) {
                    for(String string : entry.getValue()) {
                        totalLength += string.getBytes().length;
                    }
                }
                spotifyAccounts.setRequestProperty("Content-Length", String.valueOf(totalLength));
                spotifyAccounts.connect();
            } catch(IOException ex) {}

每次收到 411 错误,我都会输出 Content-Length 请求属性的值,如下所示:

int code = 0;
            try {
                code = spotifyAccounts.getResponseCode();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            if(code != 200) {
                if(code == 411) {
                    System.out.println("Spotify Accounts - Content-Length: " + spotifyAccounts.getContentLength());
                    System.out.println("Spotify Accounts - Connecting User Agent: " + spotifyAccounts.getRequestProperty("User-Agent"));
                }
                // Other error code handling
            }

输出 Content-Length 始终返回 1564 的值。

我尝试了很多方法,例如将 HttpsURLConnection 更改为 HttpURLConnection、更改 Content-Type 标头以及直接将 Content-Length 设置为 0。但是所有这些解决方案仍然返回 411 Length Required 错误。是我遗漏了什么,还是我误解了什么?

【问题讨论】:

    标签: java api authorization spotify


    【解决方案1】:

    spotify 文档说您需要在请求正文中设置grant_type,但是?grant_type=client_credentials 这不起作用。 相反,您可以提交这样的正文:

        byte[] input = "grant_type=client_credentials".getBytes(StandardCharsets.UTF_8);
        try (OutputStream os = connection.getOutputStream()) {
            os.write(input);
        }
    

    【讨论】:

      猜你喜欢
      • 2020-10-06
      • 2014-08-13
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 2021-07-04
      • 2017-12-29
      • 2018-08-12
      相关资源
      最近更新 更多