spotify
一个不完整的 Dart 库,用于与 Spotify Web API 进行交互。
一个用于与 Spotify API 交互的 Dart 库。
^1.6.0^4.9.0^2.0.5^1.17.0^6.0.0^1.28.0^2.10.4^6.11.3以下为英文项目原文快照,最新内容请访问 GitHub。
A Dart library for interfacing with the Spotify API.
Installation | Features | Quick Start | Authorization | Features & Bugs | Contributing
Add spotify to your pubspec.yaml:
dependencies:
spotify: ^0.16.1
Alternatively, run
dart pub add spotify
and the latest version will be added automatically.
Finally, run dart pub get to install the package.
This library provides comprehensive access to Spotify's Web API:
me endpointPages<T> and CursorPages<T>Note: Related Artists, Audio Features, Audio Analysis, Featured Playlists, Category Playlists and Recommendation endpoints are deprecated by Spotify but still available in this library.
import 'package:spotify/spotify.dart';
void main() async {
final credentials = SpotifyApiCredentials(clientId, clientSecret);
final spotify = SpotifyApi(credentials);
// Get artist info
final artist = await spotify.artists.get('0OdUWJ0sBjDrqHygGUXeCF');
print(artist.name); // 'Band of Horses'
// Search for tracks
var search = await spotify.search.get('metallica').first(2);
print(search.first.items); // List of albums, artists, tracks, etc.
}
Many endpoints return paginated results:
// Get all tracks from an album
var tracks = await spotify.albums.getTracks(albumId).all();
// Get first N items
var featured = await spotify.playlists.featured.first(5);
// Stream through pages
await for (var page in spotify.playlists.getUsersPlaylists(userId).stream()) {
print(page.items);
}
This library supports multiple authorization flows:
For detailed information on authorization flows, OAuth scopes, and advanced usage, see the wiki.
For mobile apps and single-page applications that cannot securely store client secrets, use PKCE:
// Generate a secure code verifier
final verifier = SpotifyApi.generateCodeVerifier();
// Create PKCE credentials (no client secret needed!)
final credentials = SpotifyApiCredentials.pkce(
'your-client-id',
codeVerifier: verifier,
);
// Complete OAuth flow
final grant = SpotifyApi.authorizationCodeGrant(credentials);
final authUri = grant.getAuthorizationUrl(redirectUri, scopes: scopes);
// ... redirect user, get authorization code
final client = await grant.handleAuthorizationResponse(params);
final spotify = SpotifyApi.fromClient(client);
// Save credentials for later (including codeVerifier!)
final creds = await spotify.getCredentials();
// Store creds.codeVerifier along with other credential fields
// Restore later
final spotify = await SpotifyApi.asyncFromCredentials(restoredCreds);
See example/example_pkce.dart for a complete working example.
Please file feature requests and bugs at the issue tracker.
Contributions are welcome! Here's how to work on this project:
dart pub get to install dependenciesThis project uses json_serializable for model serialization:
dart run build_runner build to generate serializersdart run build_runner watch to continuously rebuild when files changeBefore committing, ensure your code passes:
dart format .dart analyzeRun the test suite:
dart test
To run the basic example:
dart run example/example.dart
You'll need to modify the file to include your Spotify client ID and secret.
For the authenticated example (accessing user data, playback control) run:
dart run example/example_auth.dart
This example will prompt you for a redirect URL and guide you through the OAuth flow to access features like currently playing tracks and available devices.