forked from LouisAldorio/go-tiktok
/
auth.go
54 lines (50 loc) · 1.04 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
package gotiktok
import (
"context"
"fmt"
"net/http"
"net/url"
)
func (c *Client) GenerateAuthURL(
serviceId string,
state string,
) string {
return fmt.Sprintf(
ServiceBaseURL+"/open/authorize?service_id=%s&state=%s",
serviceId, state,
)
}
func (c *Client) GetAccessToken(ctx context.Context, code string) (resp AccessTokenResponse, err error) {
param := url.Values{}
param.Set("app_key", c.appKey)
param.Set("app_secret", c.appSecret)
param.Set("auth_code", code)
param.Set("grant_type", "authorized_code")
err = c.requestJson(
ctx,
http.MethodGet,
AuthBaseURL,
"/api/v2/token/get",
param,
nil,
&resp,
)
return
}
func (c *Client) RefreshToken(ctx context.Context, refreshToken string) (resp AccessTokenResponse, err error) {
param := url.Values{}
param.Set("app_key", c.appKey)
param.Set("app_secret", c.appSecret)
param.Set("refresh_token", refreshToken)
param.Set("grant_type", "refresh_token")
err = c.requestJson(
ctx,
http.MethodGet,
AuthBaseURL,
"/api/v2/token/refresh",
param,
nil,
&resp,
)
return
}