remove support for oauth.

Only oauth proxy is now supported
This commit is contained in:
2023-09-26 01:16:01 +02:00
parent 1c9706485c
commit 6754b778c8
5 changed files with 33 additions and 108 deletions

View File

@@ -13,14 +13,11 @@ type alarm struct {
}
type MorningAlarmConfig struct {
SpotifyClientID string `json:"spotifyClientId"`
SpotifyClientSecret string `json:"spotifyClientSecret"`
DeviceName string `json:"deviceName"`
PlaylistID string `json:"wakeupContext"`
RedirectURL string `json:"redirectUrl"`
ListenAddr string `json:"listenAddr"`
OAuthProxyURL string `json:"oauthProxyUrl"`
OAuthProxyAPIKey string `json:"oauthProxyApiKey"`
DeviceName string `json:"deviceName"`
PlaylistID string `json:"wakeupContext"`
ListenAddr string `json:"listenAddr"`
OAuthProxyURL string `json:"oauthProxyUrl"`
OAuthProxyAPIKey string `json:"oauthProxyApiKey"`
}
type MorningAlarm struct {
@@ -44,11 +41,7 @@ func (ma *MorningAlarm) Start() {
ma.ro = gin.Default()
if ma.config.OAuthProxyURL != "" {
ma.setupSpotifyWithProxy()
} else {
ma.setupSpotify()
}
ma.setupSpotifyWithProxy()
ma.setupWebserver()

View File

@@ -2,20 +2,13 @@ package morningalarm
import (
"context"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/zmb3/spotify/v2"
spotifyauth "github.com/zmb3/spotify/v2/auth"
"golang.org/x/oauth2"
)
const redirectURI = "/api/spotifycb"
type headerTransport struct {
baseTransport http.RoundTripper
headers map[string]string
@@ -42,72 +35,6 @@ func (ma *MorningAlarm) setupSpotifyWithProxy() {
ma.sp = spotify.New(client, spotify.WithBaseURL(ma.config.OAuthProxyURL))
}
func (ma *MorningAlarm) setupSpotify() error {
auth := spotifyauth.New(
spotifyauth.WithRedirectURL(ma.config.RedirectURL+redirectURI),
spotifyauth.WithClientID(ma.config.SpotifyClientID),
spotifyauth.WithClientSecret(ma.config.SpotifyClientSecret),
spotifyauth.WithScopes(
spotifyauth.ScopeUserReadPrivate,
spotifyauth.ScopeUserModifyPlaybackState,
spotifyauth.ScopeStreaming,
spotifyauth.ScopeUserReadPlaybackState,
),
)
loadedTokenJSON, err := os.ReadFile("spotifyToken.json")
if err == nil {
fmt.Println("Found access token in file")
var token oauth2.Token
err = json.Unmarshal(loadedTokenJSON, &token)
if err != nil {
return err
}
ma.sp = spotify.New(auth.Client(context.Background(), &token), spotify.WithRetry(true))
newToken, err := ma.sp.Token()
if err != nil {
return err
}
tokenJSON, err := json.Marshal(newToken)
if err != nil {
return err
}
os.WriteFile("spotifyToken.json", tokenJSON, os.ModePerm)
return nil
}
state := "abc123"
ma.ro.GET(redirectURI, func(c *gin.Context) {
token, err := auth.Token(c, state, c.Request)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
tokenJSON, err := json.Marshal(token)
os.WriteFile("spotifyToken.json", tokenJSON, os.ModePerm)
ma.sp = spotify.New(auth.Client(c, token))
})
url := auth.AuthURL(state)
fmt.Println("Please log in to Spotify by visiting the following page in your browser:", url)
return nil
}
// playWakeUpMusic plays the playlist specified in the config at the specified device.
// Returns true if the playback was started successfully.
// Returns error if any error occurred.