新增抖音授权登录
This commit is contained in:
73
platformapi/tiktok/tiktok.go
Normal file
73
platformapi/tiktok/tiktok.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package tiktok
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (a *API) GetAppID() string {
|
||||
return a.clientKey
|
||||
}
|
||||
|
||||
func (a *API) GetSecret() string {
|
||||
return a.clientSecret
|
||||
}
|
||||
|
||||
func New(clientSecret, clientKey string, config ...*platformapi.APIConfig) *API {
|
||||
curConfig := platformapi.DefAPIConfig
|
||||
if len(config) > 0 {
|
||||
curConfig = *config[0]
|
||||
}
|
||||
return &API{
|
||||
clientSecret: clientSecret,
|
||||
clientKey: clientKey,
|
||||
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
||||
config: &curConfig,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *API) AccessAPI(baseUrl, actionApi, method string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) {
|
||||
// 序列化
|
||||
data, err := json.Marshal(bizParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 全路径请求参数
|
||||
fullURL := utils.GenerateGetURL(baseUrl, actionApi, nil)
|
||||
|
||||
// 发送请求
|
||||
sendUrl := func() *http.Request {
|
||||
var request *http.Request
|
||||
if http.MethodPost == method {
|
||||
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(string(data)))
|
||||
} else {
|
||||
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(baseUrl, actionApi, bizParams), nil)
|
||||
}
|
||||
switch actionApi {
|
||||
case TiktokTokenUrl:
|
||||
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
case TiktokRefreshTokenUrl:
|
||||
request.Header.Set("Content-Type", "multipart/form-data")
|
||||
}
|
||||
|
||||
return request
|
||||
}
|
||||
|
||||
// 数据解析
|
||||
dataMarshal := func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
|
||||
retVal = jsonResult1
|
||||
return errLevel, err
|
||||
}
|
||||
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client, sendUrl, a.config, dataMarshal)
|
||||
return retVal, err
|
||||
}
|
||||
24
platformapi/tiktok/tiktok_const.go
Normal file
24
platformapi/tiktok/tiktok_const.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package tiktok
|
||||
|
||||
import (
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"net/http"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
BaseURL = "https://open.douyin.com"
|
||||
TiktokTokenUrl = "/oauth/access_token/"
|
||||
TiktokRefreshTokenUrl = "/oauth/renew_refresh_token/"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
clientSecret string // 应用唯一标识对应的密钥
|
||||
clientKey string // 应用唯一标识
|
||||
client *http.Client
|
||||
config *platformapi.APIConfig
|
||||
locker sync.RWMutex
|
||||
|
||||
msgToken string
|
||||
msgKey string
|
||||
}
|
||||
81
platformapi/tiktok/tiktok_token.go
Normal file
81
platformapi/tiktok/tiktok_token.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package tiktok
|
||||
|
||||
import (
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// OauthAccessTokenResData access_token
|
||||
type OauthAccessTokenResData struct {
|
||||
AccessToken string `json:"access_token"` // 接口调用凭证
|
||||
UnionId string `json:"union_id"` // 当且仅当该网站应用已获得该用户的userinfo授权时,才会出现该字段。
|
||||
Scope string `json:"scope"` // 用户授权的作用域(Scope),使用逗号(,)分隔,开放平台几乎几乎每个接口都需要特定的Scope。
|
||||
ExpiresIn uint64 `json:"expires_in"` // access_token接口调用凭证超时时间,单位(秒)
|
||||
OpenId string `json:"open_id"` // 授权用户唯一标识
|
||||
RefreshToken string `json:"refresh_token"` // 用户刷新access_token
|
||||
DYError
|
||||
}
|
||||
|
||||
// DYError 错误结构体
|
||||
type DYError struct {
|
||||
ErrorCode int64 `json:"error_code,omitempty"` // 错误码
|
||||
Description string `json:"description,omitempty"` // 错误码描述
|
||||
}
|
||||
|
||||
// OauthAccessTokenRes access_token
|
||||
type OauthAccessTokenRes struct {
|
||||
Data OauthAccessTokenResData `json:"data"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// 获取抖音token
|
||||
func (a *API) GetTiktokToken(code string) (*OauthAccessTokenRes, error) {
|
||||
tokenReq := make(map[string]interface{}, 4)
|
||||
tokenReq["client_secret"] = a.clientSecret
|
||||
tokenReq["code"] = code
|
||||
tokenReq["grant_type"] = "authorization_code"
|
||||
tokenReq["client_key"] = a.clientKey
|
||||
result, err := a.AccessAPI(BaseURL, TiktokTokenUrl, http.MethodPost, tokenReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
oauthAccessToken := &OauthAccessTokenRes{}
|
||||
if err := utils.Map2StructByJson(result, oauthAccessToken, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return oauthAccessToken, nil
|
||||
}
|
||||
|
||||
// OauthRefreshTokenResData 刷新access_token
|
||||
type OauthRefreshTokenResData struct {
|
||||
AccessToken string `json:"access_token"` // 接口调用凭证
|
||||
Scope string `json:"scope"` // 用户授权的作用域
|
||||
ExpiresIn uint64 `json:"expires_in"` // 过期时间,单位(秒)
|
||||
OpenId string `json:"open_id"` // 当前应用下,授权用户唯一标识
|
||||
RefreshToken string `json:"refresh_token"` // 用户刷新
|
||||
DYError
|
||||
}
|
||||
|
||||
// OauthRefreshTokenRes 刷新access_token
|
||||
type OauthRefreshTokenRes struct {
|
||||
Data OauthRefreshTokenResData `json:"data"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// 刷新用户Refresh token
|
||||
func (a *API) RefreshToken(refreshToken string) (*OauthRefreshTokenRes, error) {
|
||||
tokenReq := make(map[string]interface{}, 2)
|
||||
tokenReq["client_key"] = a.clientKey
|
||||
tokenReq["refresh_token"] = refreshToken
|
||||
// 通过旧的refresh_token获取新的refresh_token,调用后旧refresh_token会失效,新refresh_token有30天有效期。最多只能获取5次新的refresh_token,5次过后需要用户重新授权。
|
||||
result, err := a.AccessAPI(BaseURL, TiktokRefreshTokenUrl, http.MethodPost, tokenReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
oauthRefreshToken := &OauthRefreshTokenRes{}
|
||||
if err := utils.Map2StructByJson(result, oauthRefreshToken, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return oauthRefreshToken, nil
|
||||
}
|
||||
Reference in New Issue
Block a user