gpt4 book ai didi

android - 使用 Android SDK 的 Spotify 长期登录

转载 作者:行者123 更新时间:2023-11-30 01:09:02 24 4
gpt4 key购买 nike

我期待实现一个连接到 Spotify 的应用程序。要使用 Spotify 服务,我需要一个 token ,我可以通过 Spotify SDK 获得该 token ,但是此 token 旨在快速过期而不是长期使用。在 Web API 中,我可以从我的服务器内部进行调用以通过 refresh_token 获取新的访问 token 。我知道这必须在服务器到服务器的调用中完成。但是,我不知道应该如何管理 Android Spotify SDK 提供的信息以从服务器获取 refresh_token。我什至不知道这是否可以通过 Spotify SDK 实现,或者我是否必须实现整个授权。我在网上搜索了信息,但找不到与此主题相关的信息。希望有人能帮忙。

最佳答案

几个月前,我开发了一个也使用 Spotify 服务的应用程序。因为我想查询一首歌的音频特征,所以我不得不使用 Spotify 的 Web API,因为 Spotify 的 Android SDK 还没有提供这样的东西。当我实现这个应用程序时,我并没有考虑太多,所以每次当我需要做查询时,我都会再次获得访问 token 。您可以使用以下代码获取 token ,在 json 字符串中还带有一个 expires_in 属性,告诉您 token 将过期多少秒,因此只需稍加修改就可以轻松实现您想要的。

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 "";
}

Authorization-guide

关于android - 使用 Android SDK 的 Spotify 长期登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38619791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com