I've following CURL string to obtain a token from an 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).
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.