zoukankan      html  css  js  c++  java
  • Golang 接入钉钉 群 消息通知(签名版)

    package main
    
    import (
        "crypto/hmac"
        "crypto/sha256"
        "encoding/base64"
        "encoding/json"
        "fmt"
        "io/ioutil"
        "net/http"
        "net/url"
        "strings"
        "time"
    )
    
    
    type SimpleRequest struct {
        Text SimpleRequestContent `json:"text"`
        Msgtype string `json:"msgtype"`
    }
    
    type SimpleRequestContent struct {
        Content string `json:"content"`
    }
    var UrlAddress = "https://oapi.dingtalk.com/robot/send?access_token=f092c30d******************"
    var Secret = "SEC2d31d9a834f********************"
    func main() {
        // 设置请求头
        requestBody := SimpleRequest{
            Text: SimpleRequestContent{
                Content: "simple request",
            },
            Msgtype: "text",
        }
        reqBodyBox, _ := json.Marshal(requestBody)
        body := string(reqBodyBox)
    
        //  构建 签名
        //  把timestamp+"
    "+密钥当做签名字符串,使用HmacSHA256算法计算签名,然后进行Base64 encode,最后再把签名参数再进行urlEncode,得到最终的签名(需要使用UTF-8字符集)。
        timeStampNow := time.Now().UnixNano() / 1000000
        signStr :=fmt.Sprintf("%d
    %s", timeStampNow, Secret)
    
        hash := hmac.New(sha256.New, []byte(Secret))
        hash.Write([]byte(signStr))
        sum := hash.Sum(nil)
    
        encode := base64.StdEncoding.EncodeToString(sum)
        urlEncode := url.QueryEscape(encode)
    
        // 构建 请求 url
        UrlAddress = fmt.Sprintf("%s&timestamp=%d&sign=%s", UrlAddress, timeStampNow, urlEncode)
    
        // 构建 请求体
        request, _ := http.NewRequest("POST", UrlAddress, strings.NewReader(body))
    
        // 设置库端口
        client := &http.Client{}
    
        // 请求头添加内容
        request.Header.Set("Content-Type", "application/json")
    
        // 发送请求
        response, _ := client.Do(request)
        fmt.Println("response: ", response)
    
        // 关闭 读取 reader
        defer response.Body.Close()
    
        // 读取内容
        all, _ := ioutil.ReadAll(response.Body)
        fmt.Println("all: ", string(all))
    }
    邮箱: 1090055252@qq.com
  • 相关阅读:
    webpack-dev-server 源码
    2021前端会有什么新的变化
    父类 超类 基类 子类 派生类
    Java的权限修饰符(public,private,protected,默认friendly)
    class修饰符public、private、protected、static、abstract
    hash和签名 、证书
    前端加密解密crypto
    appid app_key app_secret
    sdk开发 、sdk与插件的区别
    CF76C
  • 原文地址:https://www.cnblogs.com/zhaoxianxin/p/14318434.html
Copyright © 2011-2022 走看看