-
Notifications
You must be signed in to change notification settings - Fork 0
/
Auth.go
49 lines (39 loc) · 1000 Bytes
/
Auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package spotify
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
type AuthRes struct {
Token string `json:"access_token"`
}
type AuthStruct struct {
Get func(ID string, secret string) (string, error)
}
func Auth() AuthStruct {
return AuthStruct{
Get: func(ID string, secret string) (string, error) {
var err2 error
res, Fetcherr := http.PostForm("https://accounts.spotify.com/api/token", url.Values{"grant_type": {"client_credentials"}, "client_id": {ID}, "client_secret": {secret}})
if Fetcherr != nil {
err2 = Fetcherr
}
if res.StatusCode != 200 {
err2 = errors.New("Spotify API Error (" + strconv.Itoa(res.StatusCode) + ") : " + CleanResponseCode(res.StatusCode))
}
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
err2 = readErr
}
structure := AuthRes{}
jsonErr := json.Unmarshal(body, &structure)
if jsonErr != nil {
err2 = jsonErr
}
return structure.Token, err2
},
}
}