[英]How do I get an authentication token in C# or VB.net
I've following CURL string to obtain a token from an URL我遵循 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')
Now, i need to obtain same result in c# (ASP.net).现在,我需要在 c# (ASP.net) 中获得相同的结果。
I've tried with this but receive always NO AUTHORIZATION我试过了,但总是没有收到授权
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
I lost something.我失去了一些东西。 Thanks.
谢谢。
Solved with this:解决了这个问题:
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
Thanks Jigar.谢谢吉格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.