spotifyauth

package
Version: v2.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// AuthURL is the URL to Spotify Accounts Service's OAuth2 endpoint.
	AuthURL = "https://accounts.spotify.com/authorize"
	// TokenURL is the URL to the Spotify Accounts Service's OAuth2
	// token endpoint.
	TokenURL = "https://accounts.spotify.com/api/token"
)
View Source
const (
	// ScopeImageUpload seeks permission to upload images to Spotify on your behalf.
	ScopeImageUpload = "ugc-image-upload"
	// ScopePlaylistReadPrivate seeks permission to read
	// a user's private playlists.
	ScopePlaylistReadPrivate = "playlist-read-private"
	// ScopePlaylistModifyPublic seeks write access
	// to a user's public playlists.
	ScopePlaylistModifyPublic = "playlist-modify-public"
	// ScopePlaylistModifyPrivate seeks write access to
	// a user's private playlists.
	ScopePlaylistModifyPrivate = "playlist-modify-private"
	// ScopePlaylistReadCollaborative seeks permission to
	// access a user's collaborative playlists.
	ScopePlaylistReadCollaborative = "playlist-read-collaborative"
	// ScopeUserFollowModify seeks write/delete access to
	// the list of artists and other users that a user follows.
	ScopeUserFollowModify = "user-follow-modify"
	// ScopeUserFollowRead seeks read access to the list of
	// artists and other users that a user follows.
	ScopeUserFollowRead = "user-follow-read"
	// ScopeUserLibraryModify seeks write/delete access to a
	// user's "Your Music" library.
	ScopeUserLibraryModify = "user-library-modify"
	// ScopeUserLibraryRead seeks read access to a user's "Your Music" library.
	ScopeUserLibraryRead = "user-library-read"
	// ScopeUserReadPrivate seeks read access to a user's
	// subsription details (type of user account).
	ScopeUserReadPrivate = "user-read-private"
	// ScopeUserReadEmail seeks read access to a user's email address.
	ScopeUserReadEmail = "user-read-email"
	// ScopeUserReadCurrentlyPlaying seeks read access to a user's currently playing track
	ScopeUserReadCurrentlyPlaying = "user-read-currently-playing"
	// ScopeUserReadPlaybackState seeks read access to the user's current playback state
	ScopeUserReadPlaybackState = "user-read-playback-state"
	// ScopeUserModifyPlaybackState seeks write access to the user's current playback state
	ScopeUserModifyPlaybackState = "user-modify-playback-state"
	// ScopeUserReadRecentlyPlayed allows access to a user's recently-played songs
	ScopeUserReadRecentlyPlayed = "user-read-recently-played"
	// ScopeUserTopRead seeks read access to a user's top tracks and artists
	ScopeUserTopRead = "user-top-read"
	// ScopeStreaming seeks permission to play music and control playback on your other devices.
	ScopeStreaming = "streaming"
)

Scopes let you specify exactly which types of data your application wants to access. The set of scopes you pass in your authentication request determines what access the permissions the user is asked to grant.

Variables

View Source
var ShowDialog = oauth2.SetAuthURLParam("show_dialog", "true")

ShowDialog forces the user to approve the app, even if they have already done so. Without this, users who have already approved the app are immediately redirected to the redirect uri.

Functions

This section is empty.

Types

type Authenticator

type Authenticator struct {
	// contains filtered or unexported fields
}

Authenticator provides convenience functions for implementing the OAuth2 flow. You should always use `New` to make them.

Example:

a := spotifyauth.New(redirectURL, spotify.ScopeUserLibaryRead, spotify.ScopeUserFollowRead)
// direct user to Spotify to log in
http.Redirect(w, r, a.AuthURL("state-string"), http.StatusFound)

// then, in redirect handler:
token, err := a.Token(state, r)
client := a.Client(token)

func New

func New(opts ...AuthenticatorOption) *Authenticator

New creates an authenticator which is used to implement the OAuth2 authorization flow.

By default, NewAuthenticator pulls your client ID and secret key from the SPOTIFY_ID and SPOTIFY_SECRET environment variables.

func (Authenticator) AuthURL

func (a Authenticator) AuthURL(state string, opts ...oauth2.AuthCodeOption) string

AuthURL returns a URL to the the Spotify Accounts Service's OAuth2 endpoint.

State is a token to protect the user from CSRF attacks. You should pass the same state to `Token`, where it will be validated. For more info, refer to http://tools.ietf.org/html/rfc6749#section-10.12.

func (Authenticator) Client

func (a Authenticator) Client(ctx context.Context, token *oauth2.Token) *http.Client

Client creates a *http.Client that will use the specified access token for its API requests. Combine this with spotify.HTTPClientOpt.

func (Authenticator) Exchange

func (a Authenticator) Exchange(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error)

Exchange is like Token, except it allows you to manually specify the access code instead of pulling it out of an HTTP request.

func (Authenticator) Token

func (a Authenticator) Token(ctx context.Context, state string, r *http.Request, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error)

Token pulls an authorization code from an HTTP request and attempts to exchange it for an access token. The standard use case is to call Token from the handler that handles requests to your application's redirect URL.

type AuthenticatorOption

type AuthenticatorOption func(a *Authenticator)

func WithClientID

func WithClientID(id string) AuthenticatorOption

WithClientID allows a client ID to be specified. Without this the value of the SPOTIFY_ID environment variable will be used.

func WithClientSecret

func WithClientSecret(secret string) AuthenticatorOption

WithClientSecret allows a client secret to be specified. Without this the value of the SPOTIFY_SECRET environment variable will be used.

func WithRedirectURL

func WithRedirectURL(url string) AuthenticatorOption

WithRedirectURL configures a redirect url for oauth flows. It must exactly match one of the URLs specified in your Spotify developer account.

func WithScopes

func WithScopes(scopes ...string) AuthenticatorOption

WithScopes configures the oauth scopes that the client should request.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL