Skip to content

Commit

Permalink
Authorization now works properly; users can login
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-pierce committed Sep 7, 2021
1 parent 4180acf commit 18cf372
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
66 changes: 48 additions & 18 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ import (
"fmt"
"github.com/zmb3/spotify/v2/auth"
"log"
"os"
"net/http"
// "os"

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

const redirectURI = "http://localhost:8080/callback"

var (
auth = spotifyauth.New(spotifyauth.WithRedirectURL(redirectURI), spotifyauth.WithScopes(spotifyauth.ScopeUserReadPrivate))
ch = make(chan *spotify.Client)
state = "abc123"
)

func AuthUser() {
Expand All @@ -18,27 +26,49 @@ func AuthUser() {
log.Fatal("Error loading .env file")
}

ctx := context.Background()
config := &clientcredentials.Config{
ClientID: os.Getenv("SPOTIFY_ID"),
ClientSecret: os.Getenv("SPOTIFY_SECRET"),
TokenURL: spotifyauth.TokenURL,
}
// first start an HTTP server
http.HandleFunc("/callback", completeAuth)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Println("Got request for:", r.URL.String())
})
go func() {
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
}()

url := auth.AuthURL(state)

// tempSplit := strings.split(url, "client_id=")
// newUrl = tempSplit[0] + "client_id=" + os.Getenv("SPOTIFY_ID")

token, err := config.Token(ctx)
fmt.Println("Please log in to Spotify by visiting the following page in your browser:\n", url)

// wait for auth to complete
client := <-ch

// use the client to make calls that require authorization
user, err := client.CurrentUser(context.Background())
if err != nil {
log.Fatalf("couldn't get token: %v", err)
log.Fatal(err)
}
fmt.Println("You are logged in as:", user.ID)
}

httpClient := spotifyauth.New().Client(ctx, token)
client := spotify.New(httpClient)
msg, page, err := client.FeaturedPlaylists(ctx)
func completeAuth(w http.ResponseWriter, r *http.Request) {
tok, err := auth.Token(r.Context(), state, r)
if err != nil {
log.Fatalf("couldn't get features playlists: %v", err)
http.Error(w, "Couldn't get token", http.StatusForbidden)
log.Fatal(err)
}

fmt.Println(msg)
for _, playlist := range page.Playlists {
fmt.Println(" ", playlist.Name)
if st := r.FormValue("state"); st != state {
http.NotFound(w, r)
log.Fatalf("State mismatch: %s != %s\n", st, state)
}

// use the token to get an authenticated client
client := spotify.New(auth.Client(r.Context(), tok))
fmt.Fprintf(w, "Login Completed!")
ch <- client
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.16
require (
github.com/PuerkitoBio/goquery v1.7.1 // indirect
github.com/joho/godotenv v1.3.0 // indirect
github.com/zmb3/spotify v1.3.0 // indirect
github.com/zmb3/spotify/v2 v2.0.0 // indirect
golang.org/x/net v0.0.0-20210903162142-ad29c8ab022f // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"billboard-scraper/auth"
//"billboard-scraper/scraper"
//"fmt"
"fmt"
)

func main() {
Expand Down

0 comments on commit 18cf372

Please sign in to comment.