update(spotify): get token to use API

This commit is contained in:
rick 2021-09-04 19:05:09 +02:00
parent 9c12084665
commit a2ef5d3b79
Signed by: Rick
GPG key ID: 2B593F087240EE99
3 changed files with 28 additions and 4 deletions

View file

@ -14,6 +14,7 @@ import (
var MyClient = &http.Client{} var MyClient = &http.Client{}
var MyResp = &RespBandcamp{} var MyResp = &RespBandcamp{}
var SpotifyAPI = &TokenUser{}
func loginSpotify(w http.ResponseWriter, r *http.Request) { func loginSpotify(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil { if err := r.ParseForm(); err != nil {
@ -71,7 +72,7 @@ func getAllTracksPlaylist(id string, offset int) (SpotifyPlaylist, error) {
} }
req.Header.Add("Accept", "application/json") req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", SpotifyAPI) req.Header.Add("Authorization", SpotifyAPI.TokenType + " " + SpotifyAPI.Token)
res, err := MyClient.Do(req) res, err := MyClient.Do(req)
if err != nil { if err != nil {
@ -226,8 +227,12 @@ func getNew(w http.ResponseWriter, r *http.Request) {
MyResp.Notfound = nil MyResp.Notfound = nil
} }
func mytest(w http.ResponseWriter, r *http.Request) { func mytoken(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, r.RequestURI) defer r.Body.Close()
err := json.NewDecoder(r.Body).Decode(&SpotifyAPI)
if err != nil {
fmt.Printf("error:", err)
}
} }
func main() { func main() {
@ -237,7 +242,7 @@ func main() {
http.HandleFunc("/back", formHandler) http.HandleFunc("/back", formHandler)
http.HandleFunc("/refresh", getNew) http.HandleFunc("/refresh", getNew)
http.HandleFunc("/spotify", loginSpotify) http.HandleFunc("/spotify", loginSpotify)
http.HandleFunc("/mytest", mytest) http.HandleFunc("/mytoken", mytoken)
fmt.Printf("Starting the server…\n") fmt.Printf("Starting the server…\n")
if err := http.ListenAndServe(":8080", nil); err != nil { if err := http.ListenAndServe(":8080", nil); err != nil {

13
static/gethash.js Normal file
View file

@ -0,0 +1,13 @@
const hash = window.location.hash.substring(1).split('&')
var datas = {}
for (let elem of hash) {
elem = elem.split('=')
datas[elem[0]] = elem[1]
}
fetch('/mytoken', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(datas)
}).then(() => {window.location.replace("/")})

View file

@ -2,6 +2,12 @@ package main
import "time" import "time"
type TokenUser struct {
Token string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn string `json:"expires_in"`
}
type BandcampAlbum struct { type BandcampAlbum struct {
find bool find bool
url string url string