1

I'm trying to implement Spotify SDK on an android app, when I click the button the Spotify activity starts just fine but when I try to login nothing happens and it logs me an Unexpected token error, I used the exact same code as in the Spotify tutorials, hope you can help

    06-29 13:40:36.677 4708-4708/com.app.project.silverbars I/chromium: [INFO:async_pixel_transfer_manager_android.cc(60)] Async pixel transfers not supported
06-29 13:40:36.789 4708-4708/com.app.project.silverbars I/chromium: [INFO:async_pixel_transfer_manager_android.cc(60)] Async pixel transfers not supported
06-29 13:40:37.505 4708-4708/com.app.project.silverbars D/dalvikvm: GC_FOR_ALLOC freed 1600K, 15% free 9975K/11700K, paused 6ms, total 6ms
06-29 13:41:02.605 4708-4708/com.app.project.silverbars I/chromium: [INFO:CONSOLE(6)] "SyntaxError: Unexpected token I
                                                                        at Object.parse (native)
                                                                        at Y (https://d2d1dxiu3v1f2i.cloudfront.net/19b92cb/js/index.js:5:6501)
                                                                        at xt (https://d2d1dxiu3v1f2i.cloudfront.net/19b92cb/js/index.js:6:14454)
                                                                        at https://d2d1dxiu3v1f2i.cloudfront.net/19b92cb/js/index.js:6:14923
                                                                        at i (https://d2d1dxiu3v1f2i.cloudfront.net/19b92cb/js/index.js:5:1297)
                                                                        at Tt (https://d2d1dxiu3v1f2i.cloudfront.net/19b92cb/js/index.js:6:14933)
                                                                        at o (https://d2d1dxiu3v1f2i.cloudfront.net/19b92cb/js/index.js:6:15632)
                                                                        at s (https://d2d1dxiu3v1f2i.cloudfront.net/19b92cb/js/index.js:7:2578)
                                                                        at https://d2d1dxiu3v1f2i.cloudfront.net/19b92cb/js/index.js:7:2750
                                                                        at f.$eval (https://d2d1dxiu3v1f2i.cloudfront.net/19b92cb/js/index.js:7:10267)", source: https://d2d1dxiu3v1f2i.cloudfront.net/19b92cb/js/index.js (6)

SpotifyActivity:

public class SpotifyMusic extends AppCompatActivity implements
        PlayerNotificationCallback, ConnectionStateCallback {

    // TODO: Replace with your client ID
    private static final String CLIENT_ID = "b51d25ed8e514fa5927c028d5827a358";
    // TODO: Replace with your redirect URI
    private static final String REDIRECT_URI = "yourcustomprotocol://callback";

    // Request code that will be passed together with authentication result to the onAuthenticationResult callback
    // Can be any integer
    private static final int REQUEST_CODE = 1337;

    private Player mPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spotify_music);

        AuthenticationRequest.Builder builder =
                new AuthenticationRequest.Builder(CLIENT_ID, AuthenticationResponse.Type.TOKEN, REDIRECT_URI);
        builder.setScopes(new String[]{"user-read-private", "streaming"});
        AuthenticationRequest request = builder.build();

        AuthenticationClient.openLoginActivity(this, REQUEST_CODE, request);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        // Check if result comes from the correct activity
        if (requestCode == REQUEST_CODE) {
            AuthenticationResponse response = AuthenticationClient.getResponse(resultCode, intent);
            if (response.getType() == AuthenticationResponse.Type.TOKEN) {
                Config playerConfig = new Config(this, response.getAccessToken(), CLIENT_ID);
                mPlayer = Spotify.getPlayer(playerConfig, this, new Player.InitializationObserver() {
                    @Override
                    public void onInitialized(Player player) {
                        mPlayer.addConnectionStateCallback(SpotifyMusic.this);
                        mPlayer.addPlayerNotificationCallback(SpotifyMusic.this);
                        mPlayer.play("spotify:track:2TpxZ7JUBn3uw46aR7qd6V");
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        Log.e("MainActivity", "Could not initialize player: " + throwable.getMessage());
                    }
                });
            }
        }
    }

    @Override
    public void onLoggedIn() {
        Log.d("MainActivity", "User logged in");
    }

    @Override
    public void onLoggedOut() {
        Log.d("MainActivity", "User logged out");
    }

    @Override
    public void onLoginFailed(Throwable error) {
        Log.d("MainActivity", "Login failed");
    }

    @Override
    public void onTemporaryError() {
        Log.d("MainActivity", "Temporary error occurred");
    }

    @Override
    public void onConnectionMessage(String message) {
        Log.d("MainActivity", "Received connection message: " + message);
    }

    @Override
    public void onPlaybackEvent(EventType eventType, PlayerState playerState) {
        Log.d("MainActivity", "Playback event received: " + eventType.name());
        switch (eventType) {
            // Handle event type as necessary
            default:
                break;
        }
    }

    @Override
    public void onPlaybackError(ErrorType errorType, String errorDetails) {
        Log.d("MainActivity", "Playback error received: " + errorType.name());
        switch (errorType) {
            // Handle error type as necessary
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        // VERY IMPORTANT! This must always be called or else you will leak resources
        Spotify.destroyPlayer(this);
        super.onDestroy();
    }
}
users at 4325010
  • 3,596
  • 4
  • 28
  • 46
Andres Rodriguez
  • 199
  • 1
  • 5
  • 13
  • Your redirect_uri should match with the redirect_uri mentioned under my apps on the Spotify Developer Console. – Malav Shah Jul 18 '16 at 22:02

1 Answers1

1

What you get is actually a JSON error at JSON.parse()

Throws a SyntaxError exception if the string to parse is not valid JSON.

After looking at your code, I can see that you have not followed precisely the tutorial.There is stated

The completed code of MainActivity.java should now look like this, but with your own client ID and redirect URI:

Because your redirect URI is not set

private static final String REDIRECT_URI = "yourcustomprotocol://callback";

your URI can't get parsed. Which is not by coincidence related to your error, since the redirect URI is used in the login process and basically is the location the client will get send to after a successful account authorization.

Now about how you could set it up, I couldn't help you much since I haven't worked with the Spotify SDK, but you could use something like

private static final String REDIRECT_URI = "http://localhost:PORT_NUMBER://authenticationResponse"

PORT_NUMBER can be anything greater than 1024

For more info, this question might be helpful: Spotify redirect URI.

Hopefully you will get this one sorted out.

Community
  • 1
  • 1
users at 4325010
  • 3,596
  • 4
  • 28
  • 46
  • i really dont understand the "REDIRECT_URI", why do i need a localhost for parse the json? do i have to create another file for parse the redirect uri? – Andres Rodriguez Jun 29 '16 at 19:52