微信客服API接口对接-获取access_token-调用其他接口时都需要获取-【唯一客服】
调用任何其他接口的时候,都需要先获取access_token
并且不能频繁调用,需要有缓存机制
package wechat_kf_sdk import ( "bytes" "encoding/json" "encoding/xml" "errors" "fmt" "github.com/patrickmn/go-cache" "io" "io/ioutil" "log" "net/http" "sync" "time" ) // 定义获取access_token的响应数据结构体 type AccessTokenResponse struct { ErrCode int `json:"errcode"` // 错误码 ErrMsg string `json:"errmsg"` // 错误信息 AccessToken string `json:"access_token"` // access_token ExpiresIn int `json:"expires_in"` // 过期时间 } // 定义微信客服API的封装结构体 type KefuWework struct { corpid string // 企业ID corpsecret string // 企业密钥 Token string // 令牌 EncodingAESKey string // AES加密密钥 mutex sync.Mutex // 互斥锁,用于保护access_token的获取 } var weworkCache = cache.New(5*time.Minute, 10*time.Minute) // 缓存,用于存储access_token // 创建微信客服API的封装结构体实例 func NewKefuWework(corpid string, corpsecret, Token, EncodingAESKey string) *KefuWework { return &KefuWework{ corpid: corpid, corpsecret: corpsecret, Token: Token, EncodingAESKey: EncodingAESKey, } } // 获取access_token的函数 func (s *KefuWework) GetAccessToken() (string, error) { // 加锁,避免并发调用获取access_token接口 s.mutex.Lock() defer s.mutex.Unlock() // 判断access_token是否过期,如果未过期则直接返回 cacheKey := "wework_access_" + s.corpid if accessToken, ok := weworkCache.Get(cacheKey); ok { return accessToken.(string), nil } // 发送GET请求,构建请求URL reqURL := fmt.Sprintf("%s?corpid=%s&corpsecret=%s", "https://qyapi.weixin.qq.com/cgi-bin/gettoken", s.corpid, s.corpsecret) resp, err := http.Get(reqURL) if err != nil { return "", err } defer resp.Body.Close() // 解析响应数据到AccessTokenResponse结构体 var tokenResp AccessTokenResponse if err := json.NewDecoder(resp.Body).Decode(&tokenResp); err != nil { return "", err } // 判断获取access_token是否成功 if tokenResp.ErrCode != 0 { return "", fmt.Errorf("GetAccessToken failed: %s", tokenResp.ErrMsg) } weworkCache.Set(cacheKey, tokenResp.AccessToken, time.Duration(tokenResp.ExpiresIn-3600)*time.Second) log.Printf("GetAccessToken kefuWework:%s\n", tokenResp.AccessToken) // 返回access_token return tokenResp.AccessToken, nil }
测试用例
func TestGetAccessToken(t *testing.T) { corpid := "xx" corpsecret := "xxxxxxxxxxx" // 创建微信客服API的封装结构体实例 kefuWework := NewKefuWework(corpid, corpsecret, "", "") // 获取access_token accessToken, err := kefuWework.GetAccessToken() if err != nil { fmt.Println(err) return } fmt.Println(accessToken) }
可以正确拿到access_token
十年开发经验程序员,离职全心创业中,历时三年开发出的产品《唯一客服系统》
一款基于Golang+Vue开发的在线客服系统,软件著作权编号:2021SR1462600。一套可私有化部署的网站在线客服系统,编译后的二进制文件可直接使用无需搭开发环境,下载zip解压即可,仅依赖MySQL数据库,是一个开箱即用的全渠道在线客服系统,致力于帮助广大开发者/公司快速部署整合私有化客服功能。
开源地址:唯一客服(开源学习版)
官网地址:唯一客服官网
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2016-03-17 [nodejs] nodejs开发个人博客(七)后台登陆
2016-03-17 [android] 网络html查看器