Java 客户端凭据流出现错误400

Java 客户端凭据流出现错误400,java,spotify,Java,Spotify,我正在尝试对客户端凭据流进行身份验证,但一直返回错误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.clien

我正在尝试对客户端凭据流进行身份验证,但一直返回错误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() {
    }
}
package com.elliott.lyric.io;
导入org.apache.commons.codec.binary.Base64;
导入org.apache.http.*;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.HttpClientBuilder;
导入org.apache.http.message.BasicNameValuePair;
导入java.io.BufferedReader;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.List;
导入静态org.apache.http.protocol.http.USER\u代理;
/**
*由elliott于2017年5月5日创建。
*/
公共类SpotifyLoader{
字符串nowPlayingURL=”https://api.spotify.com/v1/me/player/currently-playing";
字符串authURL=”https://accounts.spotify.com/api/token?grant_type=client_credentials";
字符串clientID=“”;
字符串d=“”;
String authScope=“用户读取当前正在播放用户读取播放状态”;
公共SpotifyLoader(){
授权();
//getRawPlaying();
}
无效授权(){
试一试{
HttpClient client=HttpClientBuilder.create().build();
System.out.println(authURL);
HttpPost=新的HttpPost(authURL);
//添加标题
post.setHeader(“用户代理”,用户代理);
post.setHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
List urlParameters=new ArrayList();
字符串idSecret=clientID+“:”+secretID;
String idSecretEncoded=新字符串(Base64.encodeBase64(idSecret.getBytes());
添加(新的BasicNameValuePair(“授权”、“基本”+idSecretEncoded));
setEntity(新的UrlEncodedFormEntity(urlParameters));
HttpResponse response=client.execute(post);
System.out.println(“响应代码:
+response.getStatusLine().getStatusCode());
BufferedReader rd=新的BufferedReader(
新的InputStreamReader(response.getEntity().getContent());
StringBuffer结果=新的StringBuffer();
字符串行=”;
而((line=rd.readLine())!=null){
结果。追加(行);
}
}捕获(例外e){
}
}
void getRawPlaying(){
}
}

尝试以下更改请求

  • 将查询字符串从url移动到请求正文,使url为
    https://accounts.spotify.com/api/token
  • 在标题中发送授权。我刚刚修改了
    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();
    }
    
    void authorize(){
    试一试{
    authURL=”https://accounts.spotify.com/api/token";
    字符串idSecret=clientID+“:”+secretID;
    String idSecretEncoded=新字符串(Base64.encodeBase64(idSecret.getBytes());
    HttpClient client=HttpClientBuilder.create().build();
    HttpPost=新的HttpPost(authURL);
    post.setHeader(“用户代理”,用户代理);
    post.setHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
    post.setHeader(“授权”、“基本”+idSecretEncoded);
    List urlParameters=new ArrayList();
    添加(新的BasicNameValuePair(“授权类型”、“客户端凭据”);
    setEntity(新的UrlEncodedFormEntity(urlParameters));
    HttpResponse response=client.execute(post);
    System.out.println(“响应代码:+Response.getStatusLine().getStatusCode());
    BufferedReader rd=新的BufferedReader(
    新的InputStreamReader(response.getEntity().getContent());
    StringBuffer结果=新的StringBuffer();
    字符串行=”;
    而((line=rd.readLine())!=null){
    结果。追加(行);
    }
    }捕获(例外e){
    e、 printStackTrace();
    }
    
    }


  • 非常感谢你,伙计。因此,我将auth放在了错误的位置,并误解了参数部分。所以它就像?授权=基本。。URL中的etc?您尝试将
    授权
    作为表单数据,将
    授权类型
    作为查询参数: