我跟随CURL字符串从URL中获得一个令牌
$(curl 'https://api.api.api.com/api/auth/auth/oauth/token' -H 'accept: application/json, text/plain, */*' -H 'authorization: Basic KZZZOnRoaJJms3NlQ3JluO==' --data 'grant_type=password&scope=webclient&username=user1.user2&password=???????????'| jq -r '.access_token')现在,我需要在c# (ASP.net)中获得相同的结果。
我试过了,但一直没有得到授权
Private Sub AuthTOKEN()
Try
Dim consumerSecret As String = "KZZZOnRoaJJms3NlQ3JluO=="
Dim accessToken As String
Dim myURL As String = "https://api.api.api.com/api/auth/auth/oauth/token"
Dim byte1 As Byte() = Encoding.ASCII.GetBytes("grant_type=password&scope=webclient&username=user1.user2&password=password")
Dim bearerReq As HttpWebRequest = TryCast(WebRequest.Create(myURL), HttpWebRequest)
bearerReq.Accept = "application/json, text/plain, */*"
bearerReq.Method = "POST"
bearerReq.ContentType = "application/x-www-form-urlencoded"
bearerReq.ContentLength = byte1.Length
bearerReq.KeepAlive = False
bearerReq.Headers.Add("Authorization", "Basic " & Convert.ToBase64String(Encoding.[Default].GetBytes(consumerSecret)))
Dim newStream As Stream = bearerReq.GetRequestStream()
newStream.Write(byte1, 0, byte1.Length)
Dim bearerResp As WebResponse = bearerReq.GetResponse()
Using reader = New StreamReader(bearerResp.GetResponseStream(), Encoding.UTF8)
Dim response = reader.ReadToEnd()
Dim bearer As Bearer = JsonConvert.DeserializeObject(Of Bearer)(response)
accessToken = bearer.access_token
End Using
Console.WriteLine(accessToken)
Console.Read()
Catch ex As Exception
MsgBox(ex.ToString)
Finally
End Try
End Sub我失去了一些东西。谢谢。
发布于 2022-06-06 14:41:02
解决了这个问题:
Private Sub AuthTOKEN()
Try
Dim consumerKey As String = "Basic"
Dim consumerSecret As String = "XXXXXXXXXXXXXXXXX=="
Dim accessToken As String
Dim myURL As String = "https://api.api.api.com/api/oauth/token"
Dim x As String
x = "grant_type=password&scope=webclient&username=user1.user2&password=xxxxx"
Dim byte1 As Byte() = Encoding.ASCII.GetBytes(x)
Dim bearerReq As HttpWebRequest = TryCast(WebRequest.Create(myURL), HttpWebRequest)
bearerReq.Accept = "application/json, text/plain, */*"
bearerReq.Method = "POST"
bearerReq.ContentType = "application/x-www-form-urlencoded"
bearerReq.ContentLength = byte1.Length
bearerReq.KeepAlive = False
bearerReq.Headers.Add("Authorization", "Basic " & consumerSecret)
Dim newStream As Stream = bearerReq.GetRequestStream()
newStream.Write(byte1, 0, byte1.Length)
Dim bearerResp As WebResponse = bearerReq.GetResponse()
Using reader = New StreamReader(bearerResp.GetResponseStream(), Encoding.UTF8)
Dim response = reader.ReadToEnd()
Dim bearer As Bearer = JsonConvert.DeserializeObject(Of Bearer)(response)
accessToken = bearer.access_token
End Using
Console.WriteLine(accessToken)
Console.Read()
Catch ex As Exception
MsgBox(ex.ToString)
Finally
End Try
End Sub谢了,吉格。
https://stackoverflow.com/questions/72517456
复制相似问题