2016-08-19 1918 views
0

由於問題所在,我想使用用戶名和密碼登錄Spotify。 我不想使用應用程序或嵌入式網絡視圖登錄,使我的應用程序更清潔。我正在使用我自己的GUI來嘗試登錄。如何使用用戶名和密碼登錄Spotify?

有沒有辦法做到這一點?我不太熟悉API調用,但我找不到方法。

回答

0

我想到了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; 

請注意,會話只是一個持有響應代碼的類。

相關問題