客户端凭据流错误400

时间:2017-05-06 15:54:15

标签: 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.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 个答案:

答案 0 :(得分:0)

尝试使用以下更改

的请求
  1. 将查询字符串从网址移至请求正文,以便网址为https://accounts.spotify.com/api/token
  2. 在标头中发送授权。我刚刚修改了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();
    }
    

    }