簡體   English   中英

如何使用用戶名和密碼登錄Spotify?

[英]How to login to Spotify with username and password?

隨着問題的發展,我想使用用戶名和密碼登錄Android中的Spotify。 我不想使用應用程序或嵌入式Web視圖進行登錄,以使我的應用程序更整潔。 我正在使用自己的GUI嘗試登錄。

有辦法嗎? 我對API調用不是很熟悉,但是找不到方法。

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

請注意,Session只是一個保存響應代碼的類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM