spotify / android-sdk

Spotify SDK for Android
https://developer.spotify.com/documentation/android/
Apache License 2.0
474 stars 120 forks source link

Authentication Service Unknown Error, login not taking place #364

Open AdvaitChirmule opened 5 months ago

AdvaitChirmule commented 5 months ago

Thanks for your interest in the Spotify App Remote SDK! If you're submitting a bug, please use the following template. If your issue is a feature request, please include your use-case so that we have all the necessary info.

Issue found on June 16, 2024.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        authenticateSpotify();
    }

    private void authenticateSpotify() {
        AuthorizationRequest.Builder builder = new AuthorizationRequest.Builder(CLIENT_ID, AuthorizationResponse.Type.CODE, REDIRECT_URI);
        builder.setScopes(new String[]{"streaming"});
        AuthorizationRequest request = builder.build();
        AuthorizationClient.openLoginActivity(this, 1337, 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 == 1337) {
            AuthorizationResponse response = AuthorizationClient.getResponse(resultCode, intent);
            switch (response.getType()) {
                // Response was successful and contains auth token
                case CODE:
                    Toast.makeText(MainActivity.this, "Here", Toast.LENGTH_SHORT).show();
                    connectToSpotifyRemote();
                    break;

                // Auth flow returned an error
                case ERROR:
                    Toast.makeText(MainActivity.this, "Authorization error: " + response.getError() + response.getCode(), Toast.LENGTH_SHORT).show();
                    break;

                // Most likely auth flow was cancelled
                default:
                    // Handle other cases
            }
        }
    }

    private void connectToSpotifyRemote() {
        ConnectionParams connectionParams = new ConnectionParams.Builder(CLIENT_ID)
                .setRedirectUri(REDIRECT_URI)
                .showAuthView(true)
                .build();

        SpotifyAppRemote.connect(this, connectionParams, new Connector.ConnectionListener() {

            public void onConnected(SpotifyAppRemote spotifyAppRemote) {
                mSpotifyAppRemote = spotifyAppRemote;
                Log.d("MainActivity", "Connected! Yay!");
                Toast.makeText(MainActivity.this, "Connected to Spotify Remote", Toast.LENGTH_SHORT).show();
                // Now you can start interacting with App Remote
                connected();
            }

            public void onFailure(Throwable throwable) {
                Toast.makeText(MainActivity.this, "Failed to connect to Spotify Remote", Toast.LENGTH_SHORT).show();
                Log.e("MainActivity", throwable.getMessage(), throwable);
                // Something went wrong when attempting to connect! Handle errors here
            }
        });

My Client ID is correct, I have used the same Redirect URI in both my dashboard and my code, I have added the SHA1 key but I just don't seem to get it authorized.

It fails at this line: AuthorizationResponse response = AuthorizationClient.getResponse(resultCode, intent); Spotify app seems to open up for a split second (cause I see the loading sign) before getting the AUTHORIZATION SERVICE UNKNOWN error. I have the app and I have logged in using the same ID. Tried on a virtual device and also my personal phone but both of them did not work. Could someone please help? I didn't find anything on the internet so I'm not sure what the issue is.

Luytenn commented 2 months ago

I'm having the same problem, did you found any solution?