displays the user's top songs via spotify api, authentication via oauth 2.
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
module cs2440.joshashton.dev/api
|
||||
|
||||
go 1.23.5
|
||||
|
||||
require github.com/joho/godotenv v1.5.1 // indirect
|
||||
@@ -0,0 +1,2 @@
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
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("../.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(":8080", nil)
|
||||
}
|
||||
Reference in New Issue
Block a user