Spotify
This is a Go wrapper for working with Spotify's
Web API.
It aims to support every task listed in the Web API Endpoint Reference,
located here.
By using this library you agree to Spotify's
Developer Terms of Use.
Installation
To install the library, simply
go get github.com/zmb3/spotify/v2
Authentication
Spotify uses OAuth2 for authentication and authorization.
As of May 29, 2017 all Web API endpoints require an access token.
You can authenticate using a client credentials flow, but this does not provide
any authorization to access a user's private data. For most use cases, you'll
want to use the authorization code flow. This package includes an Authenticator
type to handle the details for you.
Start by registering your application at the following page:
https://developer.spotify.com/my-applications/.
You'll get a client ID and secret key for your application. An easy way to
provide this data to your application is to set the SPOTIFY_ID and SPOTIFY_SECRET
environment variables. If you choose not to use environment variables, you can
provide this data manually.
auth := spotifyauth.New(spotifyauth.WithRedirectURL(redirectURL), spotifyauth.WithScopes(spotifyauth.ScopeUserReadPrivate))
url := auth.AuthURL(state)
func redirectHandler(w http.ResponseWriter, r *http.Request) {
token, err := auth.Token(r.Context(), state, r)
if err != nil {
http.Error(w, "Couldn't get token", http.StatusNotFound)
return
}
client := spotify.New(auth.Client(r.Context(), token))
}
You may find the following resources useful:
-
Spotify's Web API Authorization Guide:
https://developer.spotify.com/web-api/authorization-guide/
-
Go's OAuth2 package:
https://godoc.org/golang.org/x/oauth2/google
Helpful Hints
Automatic Retries
The API will throttle your requests if you are sending them too rapidly.
The client can be configured to wait and re-attempt the request.
To enable this, set the AutoRetry
field on the Client
struct to true
.
For more information, see Spotify rate-limits.
API Examples
Examples of the API can be found in the examples directory.
You may find tools such as Spotify's Web API Console
or Rapid API
valuable for experimenting with the API.