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
Authentication
Most of the Web API functionality is available without authenticating.
However, authenticated users benefit from increased rate limits.
Features that access a user's private data require authorization.
All functions requiring authorization are explicitly marked as
such in the godoc.
Spotify uses OAuth2 for authentication, which typically requires the user to login
via a web browser. This package includes an Authenticator
type to handle the details for you.
Start by getting 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 := spotify.NewAuthenticator(redirectURL, spotify.ScopeUserReadPrivate)
auth.SetAuthInfo(clientID, secretKey)
url := auth.AuthURL(state)
func redirectHandler(w http.ResponseWriter, r *http.Request) {
token, err := auth.Token(state, r)
if err != nil {
http.Error(w, "Couldn't get token", http.StatusNotFound)
return
}
client := auth.NewClient(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
Default Client
For API calls that require authorization, you should create your own
spotify.Client
using an Authenticator
. For calls that don't require authorization,
package level wrapper functions are provided (see spotify.Search
for example)
These functions just proxy through spotify.DefaultClient
, similar to the way
the net/http
package works.
Optional Parameters
Many of the functions in this package come in two forms - a simple version that
omits optional parameters and uses reasonable defaults, and a more sophisticated
version that accepts additional parameters. The latter is suffixed with Opt
to indicate that it accepts some optional parameters.
API Examples
Search
The search functionality returns a set of results, grouped by type (album, artist,
playlist, and track). You can search for more than one type of item with a single
search. For example, to search for holiday playlists and albums:
results, err := spotify.Search("holiday", SearchTypePlaylist|SearchTypeAlbum)
if results.Albums != nil {
for _, item := results.Albums.Albums {
fmt.Println("Album: ", item.Name)
}
}
if results.Playlists != nil {
for _, item := results.Playlists.Playlists {
fmt.Println("Playlist: ", item.Name)
}
}
The search query supports a variety of queries. Refer to the godoc for more information.
Users
To get information about another Spotify user:
user, err := spotify.GetUsersPublicProfile("user-id")
fmt.Println(user.DisplayName)
fmt.Println(user.Followers.Count, "followers")
To get information about the current user, you must authenticate first:
c := spotify.Client{}
c.HTTP = getHTTPClient()
me, err := c.CurrentUser()
fmt.Println(me.ID, me.Email, me.DisplayName)
To check if a user follows another user:
follows, err := c.CurrentUserFollows("user", "<spotify_id_here")
if follows[0] {
fmt.Println("Yes, the current user follows this user.")
} else {
fmt.Println("No, the current user does not follow this user")
}
You can check multiple items in the same call (and this works for artists too):
follows, err := c.CurrentUserFollows("artist", "artist_0_id", "artist_1_id", "artist_2_id")
if follows[0] {
fmt.Println("The user follows artist 0")
}
if follows[1] {
fmt.Println("The user follows artist 1")
}
if follows[2] {
fmt.Println("The user follows artist 2")
}
Tracks
To get catalog information for a track:
track, err := spotify.GetTrack("track_id_here")
fmt.Printf("%s is %d milliseconds long and has a popularity of %d\n",
track.Name, track.Duration, track.Popularity)
Playlists
To get a list of Spotify's featured playlists, authenticate first, and then:
msg, page, err := c.FeaturedPlaylists()
for _, playlist := range page.Playlists {
fmt.Println(playlist.Name, playlist.Owner)
}