Skip to content

Instantly share code, notes, and snippets.

@spudfkc
Last active June 2, 2016 21:07
Show Gist options
  • Save spudfkc/f1959c9a6e1e61b3de13fc755827c1fc to your computer and use it in GitHub Desktop.
Save spudfkc/f1959c9a6e1e61b3de13fc755827c1fc to your computer and use it in GitHub Desktop.
package main
import (
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
"net/http"
)
/* Simple example of getting an HTTP Client that will authenticate via Oauth2. No need for 3-legged auth. */
func OauthClient() *http.Client {
cc := clientcredentials.Config{
ClientID: "CLIENT_ID_HERE",
ClientSecret: "CLIENT_SECRET_HERE",
TokenURL: "https://accounts.spotify.com/api/token",
Scopes: []string{"playlist-read-private", "playlist-modify-private", "playlist-modify-public", "streaming"},
}
return cc.Client(oauth2.NoContext)
}
/* Test getting a user's playlists */
func TestOauthClient() {
client := OauthClient()
resp, err := client.Get("https://api.spotify.com/v1/users/spudfkc/playlists")
if err != nil || resp.StatusCode != 200 {
panic("Failed to get playlist")
}
// ... Success! Now just read the response which will include the playlists
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment