2016-07-27 105 views
1

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

回答

0

基本上,如果您需要刷新令牌(由SDK提供的令牌持续一个小时,然后您必须请求另一个令牌),则需要使用Web API来获取此令牌。在这种情况下,您不会将SDK用于身份验证过程的任何部分。从技术上讲,您不必使用服务器↔服务器组件来传递秘密来进行Spotify服务器认证步骤,这是避免在应用程序中硬编码秘密的最佳实践(安全明智),但从技术上讲,没有什么能够阻止您进行所有authentication stepsapp↔spotify服务器利用相关的网络API调用。

+0

我的问题是,请求另一个令牌不能与Android SDK来完成(或者,如果这是可以做到的,我不知道如何)。要获得另一个令牌,我必须使用SDK提供的活动。我不能在背景中这样做,这是我的目标。我目前的解决方案根本不使用SDK进行身份验证,并通过Web API实现。 –

+0

您可以通过他们的活动或网页浏览获得新的令牌。只是在他们身边处理(webview活动或其应用程序活动)的动作意图调用取决于Spotify应用程序安装在设备上。但是,使用他们的意图/活动不应该存在问题,因为一旦凭证被缓存,您就已经登录后,用户不再显示登录活动。只需一个微调半秒钟,然后你得到一个新的令牌。只要你没有要求任何额外的权限,就不需要额外的提示,而不必像以前的登录调用那样。 –

+0

这适用于用户开启屏幕的情况。我已经尝试在后台调用Activity,它不适合我。 –

1

几个月前,我开发了一款也使用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 ""; 
} 

Authorization-guide