-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_token.go
More file actions
47 lines (38 loc) · 911 Bytes
/
api_token.go
File metadata and controls
47 lines (38 loc) · 911 Bytes
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
package seafile
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
//获取AuthToken
func (cli *Client) Auth(username, password string) error {
formData := url.Values{
"username": {username},
"password": {password},
}
resp, err := http.PostForm(cli.Addr+"/api2/auth-token/", formData)
if err != nil {
return fmt.Errorf("请求错误:%s", err)
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("读取错误:%s %s", resp.Status, err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%s %s", resp.Status, string(b))
}
//TODO: 找到真正的返回结构
respInfo := map[string]string{}
err = json.Unmarshal(b, &respInfo)
if err != nil {
return fmt.Errorf("读取错误:%s", err)
}
cli.authToken = respInfo["token"]
if respInfo["token"] == "" {
return fmt.Errorf("%s", string(b))
}
return nil
}