抖音私信回复图片接口
企业号授权到开放平台
调用上传图片并发送私信消息
这样用户就可以在客服后台,直接给私信用户发送图片了
感兴趣的+ \/ : llike620
golang代码
// 获取 ClientToken func (this *Douyin) GetClientToken() (string, error) { url := "https://open.douyin.com/oauth/client_token/" // 判断access_token是否过期,如果未过期则直接返回 cacheKey := "douyin_client_token" + this.ClientKey if clientToken, ok := douyinCache.Get(cacheKey); ok { return clientToken.(string), nil } // 构建请求参数 params := map[string]interface{}{ "grant_type": "client_credential", "client_key": this.ClientKey, "client_secret": this.ClientSecret, } // 创建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) log.Println("抖音获取client_token:", string(responseBody)) if err != nil { return "", err } clientToken := gjson.Get(string(responseBody), "data.access_token").String() if clientToken != "" { douyinCache.Set(cacheKey, clientToken, 30*time.Minute) } return clientToken, nil }
// 上传图片 func (this *Douyin) ReplyImageMessage(access_token, eventType, open_id, to_user_id, conversation_id, msg_id, filePath string) (string, error) { url := "https://open.douyin.com/tool/imagex/client_upload/" clientToken, _ := this.GetClientToken() // 打开文件 file, err := os.Open(filePath) if err != nil { log.Println("Error opening file:", err) return "", err } defer file.Close() // 创建一个buffer来存储表单数据 body := &bytes.Buffer{} writer := multipart.NewWriter(body) // 添加文件到表单数据 part, err := writer.CreateFormFile("image", "filename.jpg") // 假设文件名为filename.jpg if err != nil { log.Println("Error creating form file:", err) return "", err } _, err = io.Copy(part, file) // 结束表单编码 err = writer.Close() if err != nil { log.Println("Error closing multipart writer:", err) return "", err } // 创建请求 req, err := http.NewRequest("POST", url, body) if err != nil { log.Println("Error creating request:", err) return "", err } // 设置请求头 req.Header.Add("Content-Type", writer.FormDataContentType()) req.Header.Add("access-token", clientToken) // 发送请求 client := &http.Client{} resp, err := client.Do(req) if err != nil { log.Println("Error sending request:", err) return "", err } defer resp.Body.Close() bodyBytes, _ := ioutil.ReadAll(resp.Body) log.Println("抖音上传图片:", string(bodyBytes)) image_id := gjson.Get(string(bodyBytes), "image_id").String() if image_id == "" { return "", errors.New("抖音上传图片失败") } url2 := "https://open.douyin.com/im/send/msg/?open_id=" + open_id // 构建请求参数 params := map[string]interface{}{ "content": map[string]interface{}{ "msg_type": 2, "image": map[string]interface{}{ "media_id": image_id, }, }, "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", url2, 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) log.Println("调用抖音私信图片接口:", string(responseBody)) if err != nil { return "", err } return string(responseBody), nil }
标签:私信,return,string,err,nil,开放平台,Println,id,图片 From: https://www.cnblogs.com/taoshihan/p/18384450