zmb3 / spotify

A Go wrapper for the Spotify Web API
Apache License 2.0
1.37k stars 287 forks source link

playlists unauthorized #91

Closed tcurdt closed 5 years ago

tcurdt commented 5 years ago

I don't quite understand why this is giving an "unauthorized" error. What am I missing? The credentials work fine for some other client.

package main

import (
  "context"
  "fmt"
  "log"

  "github.com/zmb3/spotify"
  "golang.org/x/oauth2/clientcredentials"
)

func main() {
  config := &clientcredentials.Config{
    ClientID:     os.Getenv("SPOTIFY_ID"),
    ClientSecret: os.Getenv("SPOTIFY_SECRET"),
    TokenURL:     spotify.TokenURL,
  }
  token, err := config.Token(context.Background())
  if err != nil {
    log.Fatalf("couldn't get token: %v", err)
  }

  client := spotify.Authenticator{}.NewClient(token)

  page, err := client.CurrentUsersPlaylists()
  if err != nil {
    log.Fatalf("couldn't get playlists: %v", err)
  }

  for _, playlist := range page.Playlists {
    fmt.Println("  ", playlist.Name)
  }
}
tcurdt commented 5 years ago

OK, so this needs the authorization code flow to provide the proper scopes.