Static CreateTime As Date '记录最近一次生成access_token的时间
Static AccessTocken As String '记录最近一次生成的access_token
Dim tp As TimeSpan = Date.Now - CreateTime
If tp.TotalSeconds > 2500000 Then
Dim hc As New HttpClient("https://aip.baidubce.com/oauth/2.0/token")
hc.FormData.Add("grant_type","client_credentials")
hc.FormData.Add("client_id", Vars("APIKey"))
hc.FormData.Add("client_secret", Vars("SecretKey"))
Dim ret As String = hc.GetData()
If ret = "" Then '如果失败,再尝试一次
ret = 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("error_description").ToString)
End If
End If
Return AccessTocken