如何使用用户名和密码登录Spotify?
问题描述:
由于问题所在,我想使用用户名和密码登录Spotify。 我不想使用应用程序或嵌入式网络视图登录,使我的应用程序更清洁。我正在使用我自己的GUI来尝试登录。如何使用用户名和密码登录Spotify?
有没有办法做到这一点?我不太熟悉API调用,但我找不到方法。
答
我想到了Spotify API。这是主流程:
Session session = new Session();
try {
String response = "";
URL url = new URL(GET_TOKEN_URL);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod(POST);
String credentials = params[0]+":"+params[1];
Log.d(getClass().getName(), credentials);
String basicAuth ="Basic "+new String(android.util.Base64.encode(credentials.getBytes(), Base64.DEFAULT));
Log.d(getClass().getSimpleName(), basicAuth);
httpURLConnection.setRequestProperty(AUTH,basicAuth);
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter(GRANT_TYPE, CLIENT_CREDENTIALS);
String query = builder.build().getEncodedQuery();
OutputStream os = httpURLConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
httpURLConnection.connect();
int responseCode=httpURLConnection.getResponseCode();
session.setResponseCode(responseCode);
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
while ((line=br.readLine()) != null) {
response+=line;
}
}else;{
Log.e(getClass().getSimpleName(), "ERROR:" + responseCode);
response = "Something went wrong";
}
Log.d(getClass().getSimpleName(), response);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
}
return session;
请注意,会话只是一个持有响应代码的类。