我期待着实现一个连接到Spotify的应用程序。要使用Spotify服务,我需要一个令牌,我可以通过Spotify SDK获得它,但是这个令牌旨在快速过期,而不是长期使用。在web API中,我可以从服务器内部进行调用,通过refresh_token获取新的访问令牌。我知道这必须在呼叫服务器到服务器中完成。但是,我不知道我应该如何管理Android Spotify SDK提供的信息来从服务器获取refresh_token。我甚至不知道这是否可以通过Spotify SDK实现,或者我是否必须实现整个授权。我已经在网上搜索了信息,但我找不到与此主题相关的信息。希望有人能帮上忙。
发布于 2016-10-03 13:20:20
几个月前,我开发了一款应用,也使用了Spotify的服务。因为我想查询一首歌曲的音频特征,所以我不得不使用Spotify的Web API,因为Spotify的Android SDK还没有提供这样的功能。当我实现这个应用程序时,我没有过多地考虑它,所以每次当我需要进行查询时,我都会再次获得访问令牌。您可以使用以下代码获取令牌,在json字符串中还附带了一个expires_in属性,该属性告诉您令牌将过期的秒数,因此只需稍加修改就可以轻松实现您想要的结果。
private String getAccessTokenFromJsonStr(String spotifyJsonStr) throws JSONException {
final String OWM_ACCESS_TOKEN = "access_token";
String accessToken;
try {
JSONObject spotifyJson = new JSONObject(spotifyJsonStr);
accessToken = spotifyJson.getString(OWM_ACCESS_TOKEN);
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
}
return accessToken;
}
private String getSpotifyAccessToken(){
String response;
String accessToken;
try {
String serviceURL = "https://accounts.spotify.com/api/token";
URL myURL = new URL(serviceURL);
HttpsURLConnection myURLConnection = (HttpsURLConnection) myURL.openConnection();
String userCredentials = "YOUR_USER_CREDENTIALS:YOUR_USER_CREDENTIALS";
int flags = Base64.NO_WRAP | Base64.URL_SAFE;
byte[] encodedString = Base64.encode(userCredentials.getBytes(), flags);
String basicAuth = "Basic " + new String(encodedString);
myURLConnection.setRequestProperty("Authorization", basicAuth);
myURLConnection.setRequestMethod("POST");
myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
myURLConnection.setUseCaches(false);
myURLConnection.setDoInput(true);
myURLConnection.setDoOutput(true);
System.setProperty("http.agent", "");
HashMap postDataParams = new HashMap<String, String>();
postDataParams.put("grant_type", "client_credentials");
OutputStream os = myURLConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
response = "";
int responseCode=myURLConnection.getResponseCode();
Log.d(LOG_TAG, "response code is " + responseCode);
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
while ((line=br.readLine()) != null) {
response+=line;
}
}
else {
response="";
String errLine;
String errResponse = "";
BufferedReader br=new BufferedReader(new InputStreamReader(myURLConnection.getErrorStream()));
while ((errLine=br.readLine()) != null) {
errResponse += errLine;
}
Log.d(LOG_TAG, "error response is " + errResponse);
}
Log.d(LOG_TAG, "response is " + response);
} catch (Exception e){
e.printStackTrace();
}
String accessTokenJsonStr = response.toString();
try {
accessToken = getAccessTokenFromJsonStr(accessTokenJsonStr);
return accessToken;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
发布于 2016-08-07 13:28:17
基本上,如果您需要一个刷新令牌( SDK提供的令牌持续一个小时,然后您必须请求另一个令牌),则需要使用web api来获取此令牌。在这种情况下,您不会将SDK用于身份验证过程的任何部分。从技术上讲,你不必使用服务器api服务器组件来将秘密传递给Spotify服务器认证步骤,这是最佳实践(安全方面的),避免将秘密硬编码到你的应用程序中,但从技术上讲,没有什么能阻止你利用相关的web↔调用来执行所有的authentication steps应用程序↔spotify服务器。
https://stackoverflow.com/questions/38619791
复制相似问题