Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spotify OAuth getting“;grant“缺少U类型参数”;对于客户端\u凭据流_Java_Spotify - Fatal编程技术网

Java Spotify OAuth getting“;grant“缺少U类型参数”;对于客户端\u凭据流

Java Spotify OAuth getting“;grant“缺少U类型参数”;对于客户端\u凭据流,java,spotify,Java,Spotify,我是Spotify restful API的新用户。我有一些代码可以用于我想要使用的restful调用,但无法通过Java客户机获得工作授权。返回的错误消息是: 400:请求错误 {“错误”:“不支持的授予类型”,“错误描述”:“缺少授予类型参数”} 我的代码是: public static void main(final String[] args) { getToken(); System.out.println("<<< Finished >&g

我是Spotify restful API的新用户。我有一些代码可以用于我想要使用的restful调用,但无法通过Java客户机获得工作授权。返回的错误消息是:

400:请求错误

{“错误”:“不支持的授予类型”,“错误描述”:“缺少授予类型参数”}

我的代码是:

public static void main(final String[] args) {

    getToken();

    System.out.println("<<< Finished >>>");

}

public static String getToken() {

    final String payload = "grant_type: client_credentials";
    final String unencodedCredential = CLIENT_ID + ':' + CLIENT_SECRET;
    final String encodedCredential   = Base64.getEncoder().encodeToString(unencodedCredential.getBytes());

    String response = "";
    try {
        final URL url = new URL("https://accounts.spotify.com/api/token");
        try {
            final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setRequestProperty("Authorization", "Basic " + encodedCredential);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Accept", "application/json");
            connection.setRequestProperty("grant_type", "client_credentials");
            connection.setRequestProperty("Content-Length", "" + payload.length());
            connection.setDoInput(true);

            try (final DataOutputStream writer = new DataOutputStream(connection.getOutputStream())) {
                writer.writeChars(payload);
            }

            if (connection.getResponseCode() == 200) {
                try (final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
                    while (reader.ready()) {
                        System.out.println(reader.readLine());
                    }
                }
            } else {
                System.out.println(connection.getResponseMessage());
                try (final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()))) {
                    while (reader.ready()) {
                        System.out.println(connection.getResponseCode() + ": " + connection.getResponseMessage());
                        System.out.println(reader.readLine());
                    }
                }
            }

        } catch (final IOException x) {
            x.printStackTrace();
        }
    } catch (final MalformedURLException x) {
        x.printStackTrace();
    }

    return response;

}
publicstaticvoidmain(最终字符串[]args){
getToken();
System.out.println(“>”);
}
公共静态字符串getToken(){
最终字符串payload=“授权类型:客户端凭据”;
最终字符串unencodedCredential=CLIENT_ID+':'+CLIENT_SECRET;
最终字符串encodedCredential=Base64.getEncoder().encodeToString(unencodedCredential.getBytes());
字符串响应=”;
试一试{
最终URL=新URL(“https://accounts.spotify.com/api/token");
试一试{
最终的HttpURLConnection连接=(HttpURLConnection)url.openConnection();
connection.setRequestMethod(“POST”);
connection.setDoOutput(真);
connection.setRequestProperty(“授权”、“基本”+encodedCredential);
connection.setRequestProperty(“内容类型”、“应用程序/x-www-form-urlencoded”);
setRequestProperty(“接受”、“应用程序/json”);
setRequestProperty(“授予类型”、“客户端凭据”);
connection.setRequestProperty(“Content-Length”,“”+payload.Length());
connection.setDoInput(true);
try(final DataOutputStream writer=newdataoutputstream(connection.getOutputStream())){
writer.writeChars(有效载荷);
}
if(connection.getResponseCode()==200){
try(final BufferedReader=new BufferedReader(new InputStreamReader(connection.getInputStream())){
while(reader.ready()){
System.out.println(reader.readLine());
}
}
}否则{
System.out.println(connection.getResponseMessage());
try(final BufferedReader=new BufferedReader(new InputStreamReader(connection.getErrorStream())){
while(reader.ready()){
System.out.println(connection.getResponseCode()+”:“+connection.getResponseMessage());
System.out.println(reader.readLine());
}
}
}
}捕获(最终IOX异常){
x、 printStackTrace();
}
}捕获(最终异常异常异常异常x){
x、 printStackTrace();
}
返回响应;
}

您有没有发现问题的原因?您有没有发现问题的原因?