43 lines
848 B
Go
43 lines
848 B
Go
package main
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
"github.com/joho/godotenv"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
func generateRandomString(length int) string {
|
|
var b = make([]byte, length)
|
|
rand.Read(b)
|
|
return fmt.Sprintf("%x", b)
|
|
}
|
|
|
|
func main() {
|
|
err := godotenv.Load("../music/.env")
|
|
if err != nil {
|
|
log.Fatalf("Error loading .env file: %s", err)
|
|
}
|
|
client_id := os.Getenv("SPOTIFY_CLIENT_ID")
|
|
|
|
redirect_uri := "http://localhost/music"
|
|
|
|
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
|
|
scope := "user-top-read"
|
|
|
|
redirectURL := "https://accounts.spotify.com/authorize?" + strings.Join([]string{
|
|
"response_type=code",
|
|
"client_id=" + client_id,
|
|
"scope=" + scope,
|
|
"redirect_uri=" + redirect_uri,
|
|
}, "&")
|
|
|
|
http.Redirect(w, r, redirectURL, http.StatusFound)
|
|
})
|
|
|
|
http.ListenAndServe(":9753", nil)
|
|
}
|