1 Star 0 Fork 0

网易易盾/antispam-go-demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
livevideosolution_feedback.go 2.98 KB
一键复制 编辑 原始数据 按行查看 历史
/*
@Author : yidun_dev
@Date : 2020-10-29
@File : livevideosolution_feedback.go
@Version : 1.0
@Golang : 1.13.5
@Doc : http://dun.163.com/api.html
*/
package main
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
simplejson "github.com/bitly/go-simplejson"
"github.com/tjfoc/gmsm/sm3"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"time"
)
const (
apiUrl = "http://as.dun.163yun.com/v1/livewallsolution/feedback"
version = "v1.0"
secretId = "your_secret_id" //产品密钥ID,产品标识
secretKey = "your_secret_key" //产品私有密钥,服务端生成签名信息使用,请严格保管,避免泄露
)
//请求易盾接口
func check(params url.Values) *simplejson.Json {
params["secretId"] = []string{secretId}
params["version"] = []string{version}
params["timestamp"] = []string{strconv.FormatInt(time.Now().UnixNano()/1000000, 10)}
params["nonce"] = []string{strconv.FormatInt(rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(10000000000), 10)}
// params["signatureMethod"] = []string{"SM3"} // 签名方法支持国密SM3,默认MD5
params["signature"] = []string{genSignature(params)}
resp, err := http.Post(apiUrl, "application/x-www-form-urlencoded", strings.NewReader(params.Encode()))
if err != nil {
fmt.Println("调用API接口失败:", err)
return nil
}
defer resp.Body.Close()
contents, _ := ioutil.ReadAll(resp.Body)
result, _ := simplejson.NewJson(contents)
return result
}
//生成签名信息
func genSignature(params url.Values) string {
var paramStr string
keys := make([]string, 0, len(params))
for k := range params {
keys = append(keys, k)
}
sort.Strings(keys)
for _, key := range keys {
paramStr += key + params[key][0]
}
paramStr += secretKey
if params["signatureMethod"] != nil && params["signatureMethod"][0] == "SM3" {
sm3Reader := sm3.New()
sm3Reader.Write([]byte(paramStr))
return hex.EncodeToString(sm3Reader.Sum(nil))
} else {
md5Reader := md5.New()
md5Reader.Write([]byte(paramStr))
return hex.EncodeToString(md5Reader.Sum(nil))
}
}
func main() {
var realTimeInfos []map[string]string
realTimeInfo := map[string]string{
"taskId": "****",
"status": "100",
}
realTimeInfos = append(realTimeInfos, realTimeInfo)
jsonString, _ := json.Marshal(realTimeInfos)
params := url.Values{
"realTimeInfoList": []string{string(jsonString)},
}
ret := check(params)
code, _ := ret.Get("code").Int()
message, _ := ret.Get("msg").String()
resultArray, _ := ret.Get("result").Array()
if code == 200 {
for _, result := range resultArray {
if resultMap, ok := result.(map[string]interface{}); ok {
taskId, _ := resultMap["taskId"].(string)
r, _ := resultMap["result"].(json.Number).Int64()
if r == 0 {
fmt.Printf("SUCCESS, taskId=%s", taskId)
} else if r == 2 {
fmt.Printf("NOT EXISTS, taskId=%s", taskId)
} else if r == 1 {
fmt.Printf("SERVER ERROR, taskId=%s", taskId)
}
}
}
} else {
fmt.Printf("ERROR: code=%d, msg=%s", code, message)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/netease_yidun/antispam-go-demo.git
git@gitee.com:netease_yidun/antispam-go-demo.git
netease_yidun
antispam-go-demo
antispam-go-demo
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385