客户端凭据流错误 400

Error 400 with Client Credentials Flow

我正在尝试对 Client Credentials Flow 进行身份验证,但一直返回错误 400。我查看了可用的 API,但看不出我做错了什么。如果有人可以在正确的方向上推动我,那将是非常棒的。谢谢

package com.elliott.lyric.io;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import static org.apache.http.protocol.HTTP.USER_AGENT;

/**
 * Created by elliott on 05/05/2017.
 */
public class SpotifyLoader {

    String nowPlayingURL = "https://api.spotify.com/v1/me/player/currently-playing";
    String authURL = "https://accounts.spotify.com/api/token?grant_type=client_credentials";
    String clientID = "";
    String secretID = "";
    String authScope = "user-read-currently-playing user-read-playback-state";

    public SpotifyLoader() {
        authorize();
        //getRawPlaying();
    }

    void authorize() {
        try {

            HttpClient client = HttpClientBuilder.create().build();
            System.out.println(authURL);
            HttpPost post = new HttpPost(authURL);

// add header
            post.setHeader("User-Agent", USER_AGENT);
            post.setHeader("Content-Type", "application/x-www-form-urlencoded");

            List<NameValuePair> urlParameters = new ArrayList<>();
            String idSecret = clientID + ":" + secretID;
            String idSecretEncoded = new String(Base64.encodeBase64(idSecret.getBytes()));
            urlParameters.add(new BasicNameValuePair("Authorization", "Basic " + idSecretEncoded));

            post.setEntity(new UrlEncodedFormEntity(urlParameters));

            HttpResponse response = client.execute(post);
            System.out.println("Response Code : "
                    + response.getStatusLine().getStatusCode());

            BufferedReader rd = new BufferedReader(
                    new InputStreamReader(response.getEntity().getContent()));

            StringBuffer result = new StringBuffer();
            String line = "";
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
        } catch (Exception e) {

        }
    }

    void getRawPlaying() {
    }
}

尝试进行以下更改的请求

  1. 将查询字符串从 url 移动到请求 body 所以 URL 是 https://accounts.spotify.com/api/token
  2. 在 header 中发送授权。我刚刚修改了 authorize() 如下。

    void authorize() {
    try {
    
        authURL = "https://accounts.spotify.com/api/token";
        String idSecret = clientID + ":" + secretID;
        String idSecretEncoded = new String(Base64.encodeBase64(idSecret.getBytes()));
    
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(authURL);
    
        post.setHeader("User-Agent", USER_AGENT);
        post.setHeader("Content-Type", "application/x-www-form-urlencoded");
        post.setHeader("Authorization", "Basic " + idSecretEncoded);
    
        List<NameValuePair> urlParameters = new ArrayList<>();
        urlParameters.add(new BasicNameValuePair("grant_type", "client_credentials"));
    
        post.setEntity(new UrlEncodedFormEntity(urlParameters));
    
        HttpResponse response = client.execute(post);
        System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
    
        BufferedReader rd = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent()));
    
        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    }