-
Notifications
You must be signed in to change notification settings - Fork 15
/
auth.go
48 lines (44 loc) · 1.32 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
package tiktok
import (
"context"
"fmt"
"net/http"
)
// GenerateAuthURL generate auth url for user to login.
// Doc: https://bytedance.feishu.cn/docs/doccnROmkE6WI9zFeJuT3DQ3YOg#ckvNFO
func (c *Client) GenerateAuthURL(state string) string {
return fmt.Sprintf(
AuthBaseURL+"/oauth/authorize?app_key=%s&state=%s",
c.appKey, state,
)
}
// GetAccessToken get access token from tiktok server.
// Doc: https://bytedance.feishu.cn/docs/doccnROmkE6WI9zFeJuT3DQ3YOg#qYtWHF
func (c *Client) GetAccessToken(ctx context.Context, code string) (resp AccessTokenResponse, err error) {
var req GetAccessTokenRequest
req.AppKey = c.appKey
req.AppSecret = c.appSecret
req.AuthCode = code
req.GrantType = "authorized_code"
r := c.prepareBody(req)
err = c.request(
ctx, http.MethodPost, AuthBaseURL,
"/api/token/getAccessToken",
nil, r, &resp)
return
}
// RefreshToken refresh access token.
// Doc: https://bytedance.feishu.cn/docs/doccnROmkE6WI9zFeJuT3DQ3YOg#bG2h09
func (c *Client) RefreshToken(ctx context.Context, rk string) (resp AccessTokenResponse, err error) {
var req RefreshTokenRequest
req.AppKey = c.appKey
req.AppSecret = c.appSecret
req.RefreshToken = rk
req.GrantType = "refresh_token"
r := c.prepareBody(req)
err = c.request(
ctx, http.MethodPost, AuthBaseURL,
"/api/token/refreshToken",
nil, r, &resp)
return
}