I have some confusion with web request and newtonsoft.json, I was trying to convert stream reader to newtonsoft.json and get access token value from api. How do i get one value from stream reader to newtonsoft.json.
request example:
{
"username":"abc",
"password":"abc123"
}
response example:
{
"accessToken":"xxxxxxxxxxx",
"expires": "12355"
}
Code:
Dim accessToken As String
Dim jsonObject As String
Dim body As String = ""
Dim request As WebRequest = WebRequest.Create("http://xxx.xxx.xxx.xxx/api/getaccessToken")
body = "{""username"": ""abc"", ""password"": ""abc123""}"
request.Method = "POST"
request.ContentType = "application/json"
request.ContentLength = 0
Dim bArray As Byte() = Encoding.UTF8.GetBytes(body)
Dim dStream As Stream = request.GetRequestStream()
dStream.Write(bArray, 0, bArray.Length)
dStream.Close()
Dim streamReader As StreamReader = New StreamReader(request.GetResponse().GetResponseStream)
Dim streamR = streamReader.ReadToEnd()
jsonObject = JsonConvert.DeserializeObject(streamR)
Return jsonObject
How do i get access Token only in my code?
I have found out the solution using newtonsoft.json.linq, I can get the object in the json response.
dim bearerToken = streamR
dim jsonObject as JObject = JObject.Parse(bearerToken)
dim token as string = jsonObject.SelectToken("access_Token").ToString
return token