在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:spotify-web-api-java/spotify-web-api-java开源软件地址:https://github.com/spotify-web-api-java/spotify-web-api-java开源编程语言:Java 100.0%开源软件介绍:
Spotify Web API JavaThis is a Java wrapper/client for the Spotify Web API. Table of ContentsInstallationThe artifact is available through Maven Central via Sonatype. Or to use a snapshot of the latest commit you can use jitpack.io as described further down below. MavenLatest official release: <dependency>
<groupId>se.michaelthelin.spotify</groupId>
<artifactId>spotify-web-api-java</artifactId>
<version>7.1.0</version>
</dependency> Latest snapshot: <dependency>
<groupId>com.github.thelinmichael</groupId>
<artifactId>spotify-web-api-java</artifactId>
<version>master-SNAPSHOT</version>
</dependency> GradleLatest official release: implementation 'se.michaelthelin.spotify:spotify-web-api-java:7.1.0' Latest snapshot: implementation 'com.github.thelinmichael:spotify-web-api-java:master-SNAPSHOT' JitpackIn order to use Jitpack you need to add their repository to your Maven<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories> Gradleallprojects {
repositories {
maven { url 'https://jitpack.io' }
}
} DocumentationSee this project's Javadoc. A huge thanks to c-schuhmann for his amazing work on the documentation! General Usage// For all requests an access token is needed
SpotifyApi spotifyApi = new SpotifyApi.Builder()
.setAccessToken("taHZ2SdB-bPA3FsK3D7ZN5npZS47cMy-IEySVEGttOhXmqaVAIo0ESvTCLjLBifhHOHOIuhFUKPW1WMDP7w6dj3MAZdWT8CLI2MkZaXbYLTeoDvXesf2eeiLYPBGdx8tIwQJKgV8XdnzH_DONk")
.build();
// Create a request object with the optional parameter "market"
final GetSomethingRequest getSomethingRequest = spotifyApi.getSomething("qKRpDADUKrFeKhFHDMdfcu")
.market(CountryCode.SE)
.build();
void getSomething_Sync() {
try {
// Execute the request synchronous
final Something something = getSomethingRequest.execute();
// Print something's name
System.out.println("Name: " + something.getName());
} catch (Exception e) {
System.out.println("Something went wrong!\n" + e.getMessage());
}
}
void getSomething_Async() {
try {
// Execute the request asynchronous
final Future<Something> somethingFuture = getSomethingRequest.executeAsync();
// Do other things...
// Wait for the request to complete
final Something something = somethingFuture.get();
// Print something's name
System.out.println("Name: " + something.getName());
} catch (Exception e) {
System.out.println("Something went wrong!\n" + e.getMessage());
}
} AuthorizationPlease see Spotify's Authorization Guide too! For authorization requests the API object requires at least to have your application's client ID and client secret set as its properties. When using the authorization code flow, the application's redirect URI is required too. Those properties will then be automatically used by functions that depend on them. SpotifyApi spotifyApi = new SpotifyApi.Builder()
.setClientId("<your_client_id>")
.setClientSecret("<your_client_secret>")
.setRedirectUri("<your_redirect_uri>")
.build(); There are three ways to retrieving an access token: Client Credentials FlowUse the client credentials flow when the requests don't require permission from a specific user. This flow doesn't return a refresh token and is useful for simple requests, like fetching albums or searching for tracks. Example: ClientCredentialsExample.java Authorization Code FlowUsing the authorization code flow to retrieve an access token is necessary if the requests are bound to a specific user. Using this flow returns a refresh token, which can be used to renew the access token before it expires. This is how it works:
When you've fetched an access and refresh token, you have to add them to your API properties for automatic usage in requests. The implementer has to handle the access token's expiration. spotifyApi
.setAccessToken("<your_access_token>")
.setRefreshToken("<your_refresh_token>")
.build(); Authorization Code Flow with Proof Key for Code Exchange (PKCE)The authorization code flow with PKCE is quite like the Authorization Code Flow except that no client secret is necessary (therefore, it is a good option for mobile and desktop applications). Instead, your application should generate a code verifier and a code challenge before each authentication request. The code verifier is a cryptographically random string between 43 and 128 characters in length. It can contain letters, digits, underscores, periods, hyphens, or tildes. To generate the code challenge, your app should hash the code verifier using the SHA256 algorithm. Then, base64url encode the hash that you generated. This flow provides your app with an access token which can be refreshed, too. The steps are similar as above:
When you have fetched an access and refresh token, you have to add them to your API properties for automatic usage in requests. The implementer must handle the access token's expiration. The refresh token can be exchanged for an access token only once, after which it becomes invalid. Examples
ContributionsSee CONTRIBUTING.md.
Requirements: Java, Maven. Code OverviewThis project's main Java package is divided into four sections:
Those unit-tested parts are connected through various classes that make the API accessible for other Java projects. You can find details about specific parts or single classes in the sections below. Enumerations
Enumerations allow elements to "be of a type" and limit them to a known value set. They are currently not specified in a unique place, but are rather scrambled across the online reference. Thus, the reference only allows for construction of enum classes from this sparse information. Exceptions
Exceptions are thrown when errors occur. They are following RFC-specified HTTP status codes and are packed with a more detailed error description. Model Objects
The model objects are entities that form the API's responses in arranged formats. They are mostly specified in the Web API Object Model and in the Web API Authorization Guide. Though, unreferenced model objects exist. This project subdivides those into...
Java classes representing those model objects include private instance variables, a private constructor, but public getter methods as well as an embedded...
Requests
The request classes mirror the strucure of Spotify's Web Api endpoints. They are divided into several categories like
Tests
Unit tests ensure that implemented features work. This project's unit tests are implemented with JUnit and mockito for mocking. Fixtures
Fixtures are JSON files that represent the data returned from the API server. We use the examples directly provided by the Web API Endpoint Reference with minor tweaks. Tweaks are needed because the reference sometimes contains invalid data examples. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论