2016-07-27 37 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