zmb3 / spotify

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

invalid username for PlayerCurrentlyPlaying() #69

Closed bundyfx closed 5 years ago

bundyfx commented 6 years ago

Hello!

Simply trying to retrieve the current playing song, feel like I am running into a wall with understanding the auth mechanisms here. Maybe it is that to get the currently playing song of my own account it is considered private data? and thus client credentials don't allow it?

package main

import (
    "context"
    "fmt"
    "log"
    "os"

    "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,
        Scopes:       []string{spotify.ScopeUserReadCurrentlyPlaying},
    }

    token, err := config.Token(context.Background())
    if err != nil {
        log.Fatalf("unable to get token: %v", err)
    }

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

    song, err := client.PlayerCurrentlyPlaying()
    if err != nil {
        log.Fatalf("unable to get currently playing song: %v", err)
    }

    fmt.Println(song)
}

The error here is Invalid username

Any help would be appreciated.

Cheers!

zmb3 commented 6 years ago

Sorry for the delay in getting back to you.

The client credentials auth flow is used to machine-to-machine communication. A user is not in the loop, so tokens obtained with this flow cannot access user data. It's generally used just to get a higher rate limit.

This is documented here: https://developer.spotify.com/documentation/general/guides/authorization-guide/#client-credentials-flow

Only endpoints that do not access user information can be accessed.

You'll need to use one of the other grant types if you want to access user data.