func auth string error options cookiejar Options jar err cookiejar New

 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
func auth() (string, error) {
options := cookiejar.Options{}
jar, err := cookiejar.New(&options)
if err != nil {
log.Fatal(err)
}
client := http.Client{Jar: jar}
form := url.Values{}
form.Add("redirect", "index.php")
form.Add("login_username", cfg.NEWSTUDIO_LOGIN)
form.Add("login_password", cfg.NEWSTUDIO_PASSWORD)
form.Add("autologin", "on")
form.Add("login", "Вход")
login_url := "http://newstudio.tv/login.php"
req, _ := http.NewRequest("POST", login_url, bytes.NewBufferString(form.Encode()))
bcookie := &http.Cookie{Name: "b", Value: "b"}
req.AddCookie(bcookie)
req.Header.Set("Content-type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
log.Println("newstudio: can't auth", err)
return "", err
}
bb_data := jar.Cookies(req.URL)[0]
defer req.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
_ = body
if err != nil {
log.Println("newstudio: can't auth", err)
return "", err
}
return bb_data.Value, nil
}