Static CreateTime As Date \'记录最近一次生成access_token的时间
Static AccessTocken As String \'记录最近一次生成的access_token
Dim CorpID As String= "xw8bbc1c6c7bd8a2e7" \'企业号的CorpID
Dim Secret As String = "xx_8nTXApeZ-pofx6ECmoEbpjDhj2SKNUe5yXtr_xos" \'管理组的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