То, что я уже пробовал/проверял:
- Мой URI перенаправления — это имя моего приложения, за которым следует ://callback. У меня он тоже есть на приборной панели без каких-либо опечаток. Мой идентификатор клиента тоже правильный.
- Я загрузил правильный ключ SHA на свою панель управления.
- Я использую эмулятор Android Studio, который у меня установлен Spotify, и я вошел в систему, используя тот же идентификатор, который я использую для своего приложения Spotify Developer.
Код: Выделить всё
public class MainActivity extends AppCompatActivity {
private static final String CLIENT_ID = "xxxx";
private static final String REDIRECT_URI = "com.example.loopify_5://callback";
private SpotifyAppRemote mSpotifyAppRemote;
@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 TOKEN:
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
}
});
}
@Override
protected void onStart() {
super.onStart();
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!");
// Now you can start interacting with App Remote
connected();
}
public void onFailure(Throwable throwable) {
Log.e("MyActivity", throwable.getMessage(), throwable);
// Something went wrong when attempting to connect! Handle errors here
}
});
}
@Override
protected void onStop() {
super.onStop();
SpotifyAppRemote.disconnect(mSpotifyAppRemote);
}
private void connected() {
// Play a playlist
mSpotifyAppRemote.getPlayerApi().play("spotify:playlist:37i9dQZF1DX2sUQwD7tbmL");
// Subscribe to PlayerState
mSpotifyAppRemote.getPlayerApi()
.subscribeToPlayerState()
.setEventCallback(playerState -> {
final Track track = playerState.track;
if (track != null) {
Log.d("MainActivity", track.name + " by " + track.artist.name);
}
});
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... potify-api