以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- 获取Accesstoken函数修改 (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=149276) |
-- 作者:ifox2018 -- 发布时间:2020/4/27 22:35:00 -- 获取Accesstoken函数修改 Static CreateTime As Date \'记录最近一次生成access_token的时间 Static AccessTocken As String \'记录最近一次生成的access_token Dim CorpID As String= "ww7deed3af842576ed" \'企业号的CorpID Dim Secret As String = "sO7RjwoisX82kB_gcRY4isEU8ZC6sJVcGs6CDlz8Nkc" \'管理组的Secret Dim tp As TimeSpan = Date.Now - CreateTime If tp.TotalSeconds > 3600 Then Dim url As String = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}" Dim hc As New HttpClient(CExp(url, CorpID, Secret)) Dim ret As String = hc.GetData() If ret = "" Then \'如果失败,再尝试一次 hc.GetData() End If CreateTime = Date.Now() Dim jo As JObject = JObject.Parse(ret) If jo("access_token") IsNot Nothing Then AccessTocken = jo("access_token") Else MessageBox.show("获取access_token失败,原因:" & vbcrlf & jo.ToString) End If End If Return AccessTocken 帮助中的这个函数能正常使用。 想咨询下,如果我有多个 管理组需要实现信息推送的功能, 这段代码需要怎么修改
|
-- 作者:有点蓝 -- 发布时间:2020/4/27 23:14:00 -- CorpID和Secret 作为参数传入,改为使用字典存储AccessTocken 全局代码定义一个字典对象 public dict as new Dictionary(of String, object()) 函数试试改为 Dim CorpID As String= args(0) \'企业号的CorpID Dim Secret As String = args(1) \'管理组的Secret Dim CreateTime As Date = Date.Today.AddDays(-1) If dict.ContainsKey(Secret) CreateTime = dict(Secret)(0) End If Dim tp As TimeSpan = Date.Now - CreateTime If tp.TotalSeconds > 3600 Then Dim url As String = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}" Dim hc As New HttpClient(CExp(url, CorpID, Secret)) Dim ret As String = hc.GetData() If ret = "" Then \'如果失败,再尝试一次 hc.GetData() End If CreateTime = Date.Now() Dim jo As JObject = JObject.Parse(ret) If jo("access_token") IsNot Nothing Then If dict.ContainsKey(CorpID) dict(Secret)(0) = CreateTime dict(Secret)(1) = jo("access_token") Else dict.Add(Secret,new object() {CreateTime ,jo("access_token")}) End If Else MessageBox.show("获取access_token失败,原因:" & vbcrlf & jo.ToString) End If End If Return dict(Secret)(1) 调用 Dim access_token As String = Functions.Execute("GetAccessToken","ww7deed3af842576ed","sO7RjwoisX82kB_gcRY4isEU8ZC6sJVcGs6CDlz8Nkc") Output.Show(access_token) [此贴子已经被作者于2020/9/10 14:37:43编辑过]
|
-- 作者:ifox2018 -- 发布时间:2020/4/28 10:21:00 -- dict.Add(Secret,{CreateTime ,jo("access_token")}) 提示错误:“ 编译错误: 应为表达式 ”
|
-- 作者:有点蓝 -- 发布时间:2020/4/28 10:42:00 -- dict.Add(Secret,new object {CreateTime ,jo("access_token")}) |
-- 作者:ifox2018 -- 发布时间:2020/4/28 13:35:00 -- 还是错误 编译错误 : 应为逗号,“)”或有效的表达式继续符 |
-- 作者:有点蓝 -- 发布时间:2020/4/28 14:25:00 -- dict.Add(Secret,new object() {CreateTime ,jo("access_token")}) |
-- 作者:ifox2018 -- 发布时间:2020/4/28 21:18:00 -- 这个语句不报错 但AccessTocken = jo("access_token") 报错. 在顶部加了句 Static AccessTocken As String 不报错. 但 调用那边该怎么写 ,下面语句提示找不到自定义函数 Dim access_token As String = Functions.Execute("GetAccessToken","wwa7fc2a99dcafb796","hynxRetmbqj38i4eL6Hx4mqo9DEdKNehieDhzzDdG88") Dim ur As String = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}" \'Dim hc As new HttpClient(Cexp(ur,Functions.Execute("GetQYAccessToken"))) Dim hc As new HttpClient(Cexp(ur,Functions.Execute("access_token"))) Dim jo As New JObject \'o("touser") = "@all" jo("msgtype") = "text" jo("agentid") = 1000011 jo("text") = New JObject jo("text")("content") = "您好,这是管理软件发送的测试信息" hc.Content = jo.ToString jo = JObject.Parse(hc.GetData) If jo("errcode") = "0" Then MessageBox.show("消息发送成功!") Else MessageBox.show(jo.ToString) End If |
-- 作者:有点蓝 -- 发布时间:2020/4/28 21:47:00 -- AccessTocken = jo("access_token") 这句代码可以去掉,之前漏删除了 |
-- 作者:ifox2018 -- 发布时间:2020/4/29 13:53:00 -- 使用下面代码报错 Dim access_token As String = Functions.Execute("GetAccessToken","wwa7fc2a99dcafb796","hynxRetmbqj38i4eL6Hx4mqo9DEdKNehieDhzzDdG88") Dim ur As String = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}" \'Dim hc As new HttpClient(Cexp(ur,Functions.Execute("GetQYAccessToken"))) Dim hc As new HttpClient(Cexp(ur,Functions.Execute("access_token"))) Dim jo As New JObject jo("touser") = "computer" jo("msgtype") = "text" jo("agentid") = 1000011 jo("text") = New JObject jo("text")("content") = "您好,这是管理软件发送的测试信息" hc.Content = jo.ToString jo = JObject.Parse(hc.GetData) If jo("errcode") = "0" Then MessageBox.show("消息发送成功!") Else MessageBox.show(jo.ToString) End If |
-- 作者:有点蓝 -- 发布时间:2020/4/29 14:15:00 -- Dim access_token As String = Functions.Execute("GetAccessToken","wwa7fc2a99dcafb796","hynxRetmbqj38i4eL6Hx4mqo9DEdKNehieDhzzDdG88") Dim ur As String = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}" Dim hc As new HttpClient(Cexp(ur,access_token))
|