268 lines
6.6 KiB
Go
268 lines
6.6 KiB
Go
|
package lib
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
// 抖音
|
||
|
type Douyin struct {
|
||
|
ClientKey string
|
||
|
ClientSecret string
|
||
|
}
|
||
|
|
||
|
func NewDouyin(clientKey, clientSecret string) (*Douyin, error) {
|
||
|
m := &Douyin{
|
||
|
ClientKey: clientKey,
|
||
|
ClientSecret: clientSecret,
|
||
|
}
|
||
|
return m, nil
|
||
|
}
|
||
|
|
||
|
// 获取 access_token
|
||
|
func (this *Douyin) GetAccessToken(code string) (string, error) {
|
||
|
url := "https://open.douyin.com/oauth/access_token/"
|
||
|
|
||
|
// 构建请求参数
|
||
|
params := map[string]interface{}{
|
||
|
"grant_type": "authorization_code",
|
||
|
"client_key": this.ClientKey,
|
||
|
"client_secret": this.ClientSecret,
|
||
|
"code": code,
|
||
|
}
|
||
|
|
||
|
// 创建HTTP请求的body
|
||
|
jsonParams, err := json.Marshal(params)
|
||
|
requestBody := bytes.NewBuffer(jsonParams)
|
||
|
|
||
|
// 创建POST请求
|
||
|
req, err := http.NewRequest("POST", url, requestBody)
|
||
|
if err != nil {
|
||
|
fmt.Println("创建请求失败:", err)
|
||
|
return "", err
|
||
|
}
|
||
|
// 设置请求头
|
||
|
req.Header.Set("Content-Type", "application/json")
|
||
|
// 发送请求
|
||
|
client := http.Client{}
|
||
|
response, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
fmt.Println("发送请求失败:", err)
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
defer response.Body.Close()
|
||
|
|
||
|
// 读取响应
|
||
|
responseBody, err := ioutil.ReadAll(response.Body)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
return string(responseBody), nil
|
||
|
}
|
||
|
|
||
|
// accessToken续期
|
||
|
func (this *Douyin) RefeshAccessToken(accessToken string) (string, error) {
|
||
|
return accessToken, nil
|
||
|
}
|
||
|
|
||
|
// 刷新accessToken
|
||
|
func (this *Douyin) RefeshNewAccessToken(refreshAccessToken string) (string, error) {
|
||
|
url := "https://open.douyin.com/oauth/refresh_token/"
|
||
|
|
||
|
// 构建请求参数
|
||
|
params := fmt.Sprintf("client_key=%s&refresh_token=%s&grant_type=refresh_token", this.ClientKey, refreshAccessToken)
|
||
|
// 创建HTTP请求的body
|
||
|
requestBody := bytes.NewBuffer([]byte(params))
|
||
|
|
||
|
// 创建POST请求
|
||
|
req, err := http.NewRequest("POST", url, requestBody)
|
||
|
if err != nil {
|
||
|
fmt.Println("创建请求失败:", err)
|
||
|
return "", err
|
||
|
}
|
||
|
// 设置请求头
|
||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||
|
// 发送请求
|
||
|
client := http.Client{}
|
||
|
response, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
fmt.Println("发送请求失败:", err)
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
defer response.Body.Close()
|
||
|
|
||
|
// 读取响应
|
||
|
responseBody, err := ioutil.ReadAll(response.Body)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
return string(responseBody), nil
|
||
|
}
|
||
|
|
||
|
// 刷新refreshToken
|
||
|
func (this *Douyin) RenewRefreshToken(refreshToken string) (string, error) {
|
||
|
url := "https://open.douyin.com/oauth/renew_refresh_token/"
|
||
|
|
||
|
// 构建请求参数
|
||
|
params := fmt.Sprintf("client_key=%s&refresh_token=%s", this.ClientKey, refreshToken)
|
||
|
// 创建HTTP请求的body
|
||
|
requestBody := bytes.NewBuffer([]byte(params))
|
||
|
|
||
|
// 创建POST请求
|
||
|
req, err := http.NewRequest("POST", url, requestBody)
|
||
|
if err != nil {
|
||
|
fmt.Println("创建请求失败:", err)
|
||
|
return "", err
|
||
|
}
|
||
|
// 设置请求头
|
||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||
|
// 发送请求
|
||
|
client := http.Client{}
|
||
|
response, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
fmt.Println("发送请求失败:", err)
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
defer response.Body.Close()
|
||
|
|
||
|
// 读取响应
|
||
|
responseBody, err := ioutil.ReadAll(response.Body)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
return string(responseBody), nil
|
||
|
}
|
||
|
|
||
|
// 获取用户信息
|
||
|
func (this *Douyin) GetUserInfo(access_token, open_id string) (string, error) {
|
||
|
url := "https://open.douyin.com/oauth/userinfo/"
|
||
|
|
||
|
// 构建请求参数
|
||
|
params := fmt.Sprintf("access_token=%s&open_id=%s", access_token, open_id)
|
||
|
// 创建HTTP请求的body
|
||
|
requestBody := bytes.NewBuffer([]byte(params))
|
||
|
|
||
|
// 创建POST请求
|
||
|
req, err := http.NewRequest("POST", url, requestBody)
|
||
|
if err != nil {
|
||
|
fmt.Println("创建请求失败:", err)
|
||
|
return "", err
|
||
|
}
|
||
|
// 设置请求头
|
||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||
|
// 发送请求
|
||
|
client := http.Client{}
|
||
|
response, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
fmt.Println("发送请求失败:", err)
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
defer response.Body.Close()
|
||
|
|
||
|
// 读取响应
|
||
|
responseBody, err := ioutil.ReadAll(response.Body)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
return string(responseBody), nil
|
||
|
}
|
||
|
|
||
|
// 回复视频评论
|
||
|
func (this *Douyin) ReplyVideoComment(access_token, open_id, content, videoId, comment_id string) (string, error) {
|
||
|
url := "https://open.douyin.com/item/comment/reply/?open_id=" + open_id
|
||
|
|
||
|
// 构建请求参数
|
||
|
params := map[string]interface{}{
|
||
|
"content": content,
|
||
|
"item_id": videoId,
|
||
|
"comment_id": comment_id,
|
||
|
}
|
||
|
|
||
|
// 创建HTTP请求的body
|
||
|
jsonParams, err := json.Marshal(params)
|
||
|
requestBody := bytes.NewBuffer(jsonParams)
|
||
|
log.Println("调用抖音回复评论接口:", string(jsonParams))
|
||
|
// 创建POST请求
|
||
|
req, err := http.NewRequest("POST", url, requestBody)
|
||
|
if err != nil {
|
||
|
fmt.Println("创建请求失败:", err)
|
||
|
return "", err
|
||
|
}
|
||
|
// 设置请求头
|
||
|
req.Header.Set("Content-Type", "application/json")
|
||
|
req.Header.Set("access-token", access_token)
|
||
|
// 发送请求
|
||
|
client := http.Client{}
|
||
|
response, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
fmt.Println("发送请求失败:", err)
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
defer response.Body.Close()
|
||
|
|
||
|
// 读取响应
|
||
|
responseBody, err := ioutil.ReadAll(response.Body)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
return string(responseBody), nil
|
||
|
}
|
||
|
|
||
|
// 发送私信
|
||
|
func (this *Douyin) ReplyMessage(access_token, eventType, open_id, to_user_id, conversation_id, msg_id, content string) (string, error) {
|
||
|
url := "https://open.douyin.com/im/send/msg/?open_id=" + open_id
|
||
|
|
||
|
// 构建请求参数
|
||
|
params := map[string]interface{}{
|
||
|
"content": map[string]interface{}{
|
||
|
"msg_type": 1,
|
||
|
"text": map[string]interface{}{
|
||
|
"text": content,
|
||
|
},
|
||
|
},
|
||
|
"scene": eventType,
|
||
|
"conversation_id": conversation_id,
|
||
|
"msg_id": msg_id,
|
||
|
"to_user_id": to_user_id,
|
||
|
}
|
||
|
|
||
|
// 创建HTTP请求的body
|
||
|
jsonParams, err := json.Marshal(params)
|
||
|
requestBody := bytes.NewBuffer(jsonParams)
|
||
|
log.Println("调用抖音私信接口:", string(jsonParams))
|
||
|
// 创建POST请求
|
||
|
req, err := http.NewRequest("POST", url, requestBody)
|
||
|
if err != nil {
|
||
|
fmt.Println("创建请求失败:", err)
|
||
|
return "", err
|
||
|
}
|
||
|
// 设置请求头
|
||
|
req.Header.Set("Content-Type", "application/json")
|
||
|
req.Header.Set("access-token", access_token)
|
||
|
// 发送请求
|
||
|
client := http.Client{}
|
||
|
response, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
fmt.Println("发送请求失败:", err)
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
defer response.Body.Close()
|
||
|
|
||
|
// 读取响应
|
||
|
responseBody, err := ioutil.ReadAll(response.Body)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
return string(responseBody), nil
|
||
|
}
|