-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.go
48 lines (38 loc) · 951 Bytes
/
token.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
package main
import (
"bytes"
"net/http"
"net/url"
"github.com/rs/zerolog/log"
)
func getToken() {
log.Info().Msg("Getting token")
reqUrl := k.String("api.url") + "/login"
v := url.Values{}
//v.Set("username", k.String("api.username"))
//v.Set("password", k.String("api.password"))
r, _ := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBufferString(v.Encode()))
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Do(r)
if err != nil {
log.Error().Err(err).Msg("Error getting token")
return
}
if resp.Status != "302 Found" {
log.Error().Msg("Error getting token")
return
}
for _, cookie := range resp.Cookies() {
token = cookie.Value
}
if token == "" {
log.Error().Msg("Error getting token")
return
}
log.Info().Msg("Got token")
}