【发布时间】:2017-05-06 15:54:15
【问题描述】:
我正在尝试对客户端凭据流进行身份验证,但一直返回错误 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() {
}
}
【问题讨论】: