添加国美api,删除老版本蜂鸟API,添加抖音授权api、
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
@@ -30,44 +29,24 @@ func New(clientSecret, clientKey string, config ...*platformapi.APIConfig) *API
|
||||
}
|
||||
}
|
||||
|
||||
func (a *API) AccessAPI(baseUrl, actionApi, method string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) {
|
||||
// 序列化
|
||||
data, err := json.Marshal(bizParams)
|
||||
func (a *API) AccessAPI2(url string, params map[string]interface{}) (retVal map[string]interface{}, err error) {
|
||||
data, err := json.Marshal(params)
|
||||
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)
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||
func() *http.Request {
|
||||
request, _ := http.NewRequest(http.MethodPost, url, strings.NewReader(string(data)))
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
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 platformapi.ErrLevelSuccess, nil
|
||||
})
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
@@ -7,9 +7,10 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
BaseURL = "https://open.douyin.com"
|
||||
TiktokTokenUrl = "/oauth/access_token/"
|
||||
TiktokRefreshTokenUrl = "/oauth/renew_refresh_token/"
|
||||
BaseURLTT = "https://open.douyin.com" // 抖音
|
||||
BaseURL = "https://open.snssdk.com" // 头条
|
||||
TiktokTokenUrl = "oauth/access_token/"
|
||||
TiktokRefreshTokenUrl = "oauth/renew_refresh_token/"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
|
||||
33
platformapi/tiktok/tiktok_test.go
Normal file
33
platformapi/tiktok/tiktok_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package tiktok
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"go.uber.org/zap"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
api *API
|
||||
sugarLogger *zap.SugaredLogger
|
||||
)
|
||||
|
||||
func init() {
|
||||
logger, _ := zap.NewDevelopment()
|
||||
sugarLogger = logger.Sugar()
|
||||
baseapi.Init(sugarLogger)
|
||||
}
|
||||
|
||||
func TestCode(t *testing.T) {
|
||||
a := New("1e0d98ad7b2e9b1ba5d7529a7e17fa9b9d65380a ", "tta6a1d01c399f264201")
|
||||
data, err := a.GetTiktokToken("7aK7VOuXnlU2OIiFDFi_ssRX9vQdXtDekgW-yV77Q20rVsw8-8DX4ASbXMTA3Z-gr8tstJcgMgL04OFc-BgXB92DC6otQV6GQUiy-TzhpH109xZfZwhCKjt-OiI")
|
||||
fmt.Println(err)
|
||||
fmt.Println(data)
|
||||
}
|
||||
|
||||
func TestCode2(t *testing.T) {
|
||||
a := New("1e0d98ad7b2e9b1ba5d7529a7e17fa9b9d65380a ", "tta6a1d01c399f264201")
|
||||
data, err := a.GetTiktokToken2("CCRHRLfkl8kSHXuuZTDWQayoqHVXF2Vl4qrt173jxfWhg6Q1pZCbjRO0TzTGNPZd5vTW8UcjRupxfmEIdM06WSmAjwhvr-Ak_Pg9gJfHfrq2KvLH5eYL4C68BXM")
|
||||
fmt.Println(err)
|
||||
fmt.Println(data)
|
||||
}
|
||||
@@ -1,19 +1,20 @@
|
||||
package tiktok
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"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
|
||||
ErrorCode int64 `json:"error_code"` // 错误码
|
||||
ExpiresIn uint64 `json:"expires_in"` // access_token接口调用凭证超时时间,单位(秒)
|
||||
OpenId string `json:"open_id"` // 授权用户唯一标识
|
||||
RefreshExpiresIn int64 `json:"refresh_expires_in"` // refresh_token凭证超时时间,单位(秒)
|
||||
RefreshToken string `json:"refresh_token"` // 用户刷新access_token
|
||||
Scope string `json:"scope"` // 用户授权的作用域(Scope),使用逗号(,)分隔,开放平台几乎几乎每个接口都需要特定的Scope。
|
||||
AccessToken string `json:"access_token"` // 接口调用凭证
|
||||
Description string `json:"description"` // 错误码描述
|
||||
}
|
||||
|
||||
// DYError 错误结构体
|
||||
@@ -24,8 +25,21 @@ type DYError struct {
|
||||
|
||||
// OauthAccessTokenRes access_token
|
||||
type OauthAccessTokenRes struct {
|
||||
Data OauthAccessTokenResData `json:"data"`
|
||||
Message string `json:"message"`
|
||||
Data *OauthAccessTokenResData
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// 抖音登录返回值
|
||||
type TiktokOauthResone struct {
|
||||
ErrNo int64 `json:"err_no"`
|
||||
ErrTips string `json:"err_tips"`
|
||||
Data *struct {
|
||||
SessionKey string `json:"session_key"`
|
||||
OpenId string `json:"openid"` // 授权用户唯一标识
|
||||
AnonymousOpenid string `json:"anonymous_openid"`
|
||||
Unionid string `json:"unionid"`
|
||||
Dopenid string `json:"dopenid"`
|
||||
}
|
||||
}
|
||||
|
||||
// 获取抖音token
|
||||
@@ -35,7 +49,8 @@ func (a *API) GetTiktokToken(code string) (*OauthAccessTokenRes, error) {
|
||||
tokenReq["code"] = code
|
||||
tokenReq["grant_type"] = "authorization_code"
|
||||
tokenReq["client_key"] = a.clientKey
|
||||
result, err := a.AccessAPI(BaseURL, TiktokTokenUrl, http.MethodPost, tokenReq)
|
||||
|
||||
result, err := a.AccessAPI2(BaseURL, tokenReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -44,6 +59,28 @@ func (a *API) GetTiktokToken(code string) (*OauthAccessTokenRes, error) {
|
||||
if err := utils.Map2StructByJson(result, oauthAccessToken, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if oauthAccessToken.Data.ErrorCode != 0 {
|
||||
return nil, errors.New(oauthAccessToken.Data.Description)
|
||||
}
|
||||
return oauthAccessToken, nil
|
||||
}
|
||||
|
||||
// 获取抖音登录授权2
|
||||
func (a *API) GetTiktokToken2(code string) (*TiktokOauthResone, error) {
|
||||
tokenReq := make(map[string]interface{}, 3)
|
||||
tokenReq["appid"] = a.GetAppID()
|
||||
tokenReq["code"] = code
|
||||
tokenReq["secret"] = a.GetSecret()
|
||||
tokenReq["anonymous_code"] = ""
|
||||
result, err := a.AccessAPI2("https://developer.toutiao.com"+"/"+"api/apps/v2/jscode2session", tokenReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
oauthAccessToken := &TiktokOauthResone{}
|
||||
if err := utils.Map2StructByJson(result, oauthAccessToken, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return oauthAccessToken, nil
|
||||
}
|
||||
|
||||
@@ -69,7 +106,7 @@ func (a *API) RefreshToken(refreshToken string) (*OauthRefreshTokenRes, error) {
|
||||
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)
|
||||
result, err := a.AccessAPI2(BaseURL, tokenReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user