This commit is contained in:
邹宗楠
2022-04-14 18:30:05 +08:00
parent 13757d515d
commit ca3d08ba77
3 changed files with 175 additions and 172 deletions

View File

@@ -1,81 +1,82 @@
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_token5次过后需要用户重新授权。
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
}
//
//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_token5次过后需要用户重新授权。
// 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
//}