Static CreateTime As Date '记录最近一次生成access_token的时间
Static AccessTocken As String '记录最近一次生成的access_token
Dim Name As String = "********" '用户名
Dim PassWord As String = "********" '密码
Dim DomainName As String = "********" '账号名
Dim ScopeProjectName As String = "********" '项目
Dim tp As TimeSpan = Date.Now - CreateTime
Dim url As String = "https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens"
Dim json As String = "{""auth"":{""identity"":{""methods"":[""password""],""password"":{""user"":{""name"":""{0}"",""password"":""{1}"",""domain"":{""name"":""{2}""}}}},""scope"":{""project"":{""name"":""{3}""}}}}"
If tp.TotalSeconds > 3600 Then
Dim hc As New HttpClient(url)
hc.Headers.Add("Content-Type", "application/json;charset=utf-8")
hc.Content = CExp(json, Name, PassWord, DomainName, ScopeProjectName)
'****获取头信息
Dim retH As String = hc.GetInformation()
If retH = "" Then '如果失败,再尝试一次
hc.GetInformation()
End If
'****获取返回的信息
Dim ret As String = hc.GetData()
If ret = "" Then '如果失败,再尝试一次
hc.GetData()
End If
Dim jo As JObject = JObject.Parse(ret)
If jo("errcode") Is Nothing Then
AccessTocken = hc.ResponseHeaders("X-Subject-Token")
Output.Show("AccessTocken:" & AccessTocken)
Else
MessageBox.show("获取access_token失败,原因:" & vbcrlf & jo("errmsg").ToString)
End If
End If
Return AccessTocken