Skip to content

Commit

Permalink
Connected to spotify
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-pierce committed Sep 7, 2021
1 parent 85af59e commit 25f039c
Show file tree
Hide file tree
Showing 6 changed files with 472 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
80 changes: 80 additions & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package spotifyappauth

import (
"context"
"fmt"
"log"
"net/http"
"os"
"strings"

spotifyauth "github.com/zmb3/spotify/v2/auth"

"github.com/joho/godotenv"
"github.com/zmb3/spotify/v2"
)

// redirectURI is the OAuth redirect URI for the application.
// You must register an application at Spotify's developer portal
// and enter this value.
const redirectURI = "http://localhost:8080/callback"

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

func AuthUser() {
err := godotenv.Load()
// 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)
splitUrl := strings.Split(url, "client_id=")
clientID := "client_id=" + string(os.Getenv("SPOTIFY_ID"))

fmt.Println(os.Getenv("SPOTIFY_ID"))

tempurl := splitUrl[0] + clientID + splitUrl[1]

fmt.Println(tempurl)

fmt.Println("Please log in to Spotify by visiting the following page in your browser:", tempurl)

// 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.Fatal(err)
}
fmt.Println("You are logged in as:", user.ID)
}

func completeAuth(w http.ResponseWriter, r *http.Request) {
tok, err := auth.Token(r.Context(), state, r)
if err != nil {
http.Error(w, "Couldn't get token", http.StatusForbidden)
log.Fatal(err)
}
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
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ go 1.16

require (
github.com/PuerkitoBio/goquery v1.7.1 // indirect
github.com/joho/godotenv 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
)
Loading

0 comments on commit 25f039c

Please sign in to comment.