-
Notifications
You must be signed in to change notification settings - Fork 0
/
Auth.go
81 lines (61 loc) · 1.27 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
type token struct {
Token string `json:"access_token"`
Expire int64 `json:"expire"`
}
const (
username = "9487e8d6912cef120ae9cd5cd1038f56806f9956"
password = "IAGIT92PhSRwasDX5q3cKn2KBCrKnlA"
url = "https://api.ayoba.me/v2/login"
)
func Login() (token, error) {
data := map[string]string{
"username": username,
"password": password,
}
jsonData, err := json.Marshal(data)
if err != nil {
fmt.Print("error marshalling data ", err)
return token{}, err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Println("Error creating request:", err)
return token{}, err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return token{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Print(err)
return token{}, err
}
// fmt.Println(string(body))
var Access token
error := json.Unmarshal(body, &Access)
if error != nil {
return token{}, error
}
return Access, nil
}
// func x(){
// y, err := Login()
// if err != nil{
// fmt.Println(err)
// return
// }
// y
// }