forked from tiaguinho/mercadolibre-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
92 lines (76 loc) · 2.17 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
82
83
84
85
86
87
88
89
90
91
92
package meli
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
)
const (
version = "1.1.0"
apiRootUrl = "https://api.mercadolibre.com"
oauthUrl = "oauth/token"
)
var (
AuthUrls = map[string]string{
"MLA": "https://auth.mercadolibre.com.ar", // Argentina
"MLB": "https://auth.mercadolivre.com.br", // Brasil
"MCO": "https://auth.mercadolibre.com.co", // Colombia
"MCR": "https://auth.mercadolibre.com.cr", // Costa Rica
"MEC": "https://auth.mercadolibre.com.ec", // Ecuador
"MLC": "https://auth.mercadolibre.cl", // Chile
"MLM": "https://auth.mercadolibre.com.mx", // Mexico
"MLU": "https://auth.mercadolibre.com.uy", // Uruguay
"MLV": "https://auth.mercadolibre.com.ve", // Venezuela
"MPA": "https://auth.mercadolibre.com.pa", // Panama
"MPE": "https://auth.mercadolibre.com.pe", // Peru
"MPT": "https://auth.mercadolibre.com.pt", // Prtugal
"MRD": "https://auth.mercadolibre.com.do", // Dominicana
}
)
//getAuthUrl
func (c *Client) GetAuthUrl(redirectUri, authUrl string) (authUri string, err error) {
if authUrl != "" {
query := url.Values{}
query.Set("response_type", "code")
query.Set("client_id", strconv.Itoa(c.ClientID))
query.Set("redirect_uri", redirectUri)
authUri = fmt.Sprintf("%s/authorization?%s", authUrl, query.Encode())
} else {
err = errors.New("auth url is empty")
}
return
}
//Authorize
func (c *Client) Authorize(code, redirectUri string) (err error) {
params := map[string]string{
"grant_type": "authorization_code",
"client_id": strconv.Itoa(c.ClientID),
"client_secret": c.ClientSecret,
"code": code,
"redirect_uri": redirectUri,
}
data, mlErr := execute("POST", oauthUrl, params, nil)
if mlErr == nil {
json.Unmarshal(data, &c.MLToken)
} else {
err = mlErr
}
return
}
//RefreshAccessToken
func (c *Client) RefreshAccessToken() (err error) {
params := map[string]string{
"grant_type": "refresh_token",
"client_id": strconv.Itoa(c.ClientID),
"client_secret": c.ClientSecret,
"refresh_token": c.MLToken.RefreshToken,
}
data, mlErr := execute("POST", oauthUrl, params, nil)
if mlErr == nil {
json.Unmarshal(data, &c.MLToken)
} else {
err = mlErr
}
return
}