-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokens.go
58 lines (54 loc) · 1.49 KB
/
tokens.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
package ecobee
import(
"net/http"
"net/url"
"fmt"
"encoding/json"
)
type Tokens struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
Scope string `json:"scope"`
}
func (e *Ecobee) GetTokens() (tokens *Tokens, err error) {
authCode := url.QueryEscape(e.AuthCode)
clientId := url.QueryEscape(e.ApiKey)
url := fmt.Sprintf("https://api.ecobee.com/token?grant_type=ecobeePin&code=%s&client_id=%s", authCode, clientId)
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return tokens, err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return tokens, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&tokens)
if err != nil {
return tokens, err
}
return tokens, nil
}
func (e *Ecobee) RefreshTokens() (tokens *Tokens, err error) {
refreshToken := url.QueryEscape(e.RefreshToken)
clientId := url.QueryEscape(e.ApiKey)
url := fmt.Sprintf("https://api.ecobee.com/token?grant_type=refresh_token&refresh_token=%s&client_id=%s", refreshToken, clientId)
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return tokens, err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return tokens, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&tokens)
if err != nil {
return tokens, err
}
return tokens, nil
}