Search code examples
javaandroidspotifyokhttp

Get access token for Spotify API for Android app


I'm trying to access the spotify token with the following

OkHttpClient client = new OkHttpClient();

        String state = "azertyuiopmlkjhg";//TODO random string
        String scope = "user-read-private user-read-email";

        URIBuilder ub = new URIBuilder("https://accounts.spotify.com/authorize?")
                .addParameter("client_id", CLIENT_ID)
                .addParameter("response_type", "token")
                .addParameter("scope", scope)
                .addParameter("redirect_uri", REDIRECT_URI)
                .addParameter("state", state);
        String url = ub.toString();
        System.out.println(url);

        Request request = new Request.Builder()
                .header("Content-Type", "application/json")
                .url(url)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String myResponse = response.body().string();
                final String networkResponse = response.networkResponse().request().url().toString();
                System.out.println(myResponse);
                System.out.println(networkResponse);
                if (response.isSuccessful()) {
                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText("Success \n"+myResponse);
                        }
                    });
                }
            }
        });

The generated URL https://accounts.spotify.com/authorize?client_id=e...0&response_type=token&scope=user-read-private+user-read-email&redirect_uri=https%3A%2F%2Faccounts.spotify.com%2Fauthorize&state=azertyuiopmlkjhg redirect to the link https://accounts.spotify.com/authorize#access_token=B...g&token_type=Bearer&expires_in=3600&state=azertyuiopmlkjhg which contains the token I want

The query return should be : access_token ; token_type ; expires_in and state But I'm getting some HTML as response

<html id="app" lang="fr" dir="ltr" ng-csp ng-strict-di>
    <head>
      <meta charset="utf-8">
      <title ng-bind="(&#39;loginTitle&#39; | localize) + &#39; - Spotify&#39;">Spotify</title>
      <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
      <base href="/">
      <link rel="icon" href="https://accounts.scdn.co/sso/images/favicon.ace4d8543bbb017893402a1e9d1ac1fa.ico">
      <link href="https://accounts.scdn.co/sso/css/index.daf98bb304f1ca0e3987.css" media="screen" rel="stylesheet">
      <script defer src="https://accounts.scdn.co/sso/js/index.daf98bb304f1ca0e3987.js" sp-bootstrap></script>
      <meta ng-non-bindable sp-bootstrap-data='{"phoneFeatureEnabled":false,"previewEnabled":false,"user":false,"tpaState":"AQ...Q=","BON":["0","0",136907053]}'>
    </head>
    <body ng-controller="LoginController">
      <div ng-include="template"></div>
    </body>
    </html>

Solution

  • I ended up following the following tutorial and using the spotify authentication service

    Note that AuthenticationRequest and AuthenticationClient are now called AuthorizationRequest and AuthorizationClient