-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuth.go
67 lines (62 loc) · 1.5 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
package VkApi
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
)
type AccessToken struct {
Token string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
UserId int `json:"user_id"`
}
/*
scope := VkApi.U_OFFLINE | VkApi.U_GROUPS | VkApi.U_WALL
clientId := "3140623"
clientSecret := "VeWdmVclDCtn6ihuP1nt"
v := "5.73"
login := "[email protected]"
password := "test-password-123"
accessToken, err := VkApi.PasswordAuth(login, password, scope, clientId, clientSecret, v, nil)
// accessToken.Token - string
*/
func PasswordAuth(login, password string, scope int, clientId, clientSecret, v string, h *http.Client) (AccessToken, error) {
token := AccessToken{}
if h == nil {
h = &http.Client{Timeout: 30 * time.Second}
}
resp, err := h.PostForm(
"https://oauth.vk.com/token",
url.Values{
"grant_type": {"password"},
"client_id": {clientId},
"client_secret": {clientSecret},
"username": {login},
"password": {password},
"scope": {strconv.Itoa(scope)},
"v": {v},
"2fa_supported": {"1"},
})
if err != nil {
return token, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return token, err
}
if err := json.Unmarshal(body, &token); err != nil {
return token, err
}
if token.Token == "" {
apiError := new(ApiError)
if err := json.Unmarshal(body, &apiError); err != nil {
return token, err
} else {
return token, apiError
}
}
return token, nil
}