API using VB.NET

Solved Locked

Hi

i am trying to connect to the Mobicontrol API using VB.NET but im not getting very far.

Has anybody done this? 

Are there any examples?

It doesnt matter what i try i always get the response 'Bad Request'

thanks

gareth

5 years ago
SOTI MobiControl
ANSWERS
GP
Gareth Perry
5 years ago

Hi,

There are probably more elegant / efficient solutions but the code i have used to return the token is below. i have used the Newtonsoft Json package to help parse the response.

Function access_token()
Dim wHeader As WebHeaderCollection = New WebHeaderCollection()

wHeader.Clear()
Dim clientid As String = "Enter your client id"
Dim clientsecret As String = "Enter your client secret"
Dim idsecret As String
idsecret = clientid + ":" + clientsecret
Dim encodedidsecret = System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(idsecret))
Dim token As String


Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create("https://Enter you domain/MobiControl/api/token")
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"


Dim outgoingQueryString As NameValueCollection = HttpUtility.ParseQueryString(String.Empty)
outgoingQueryString.Add("grant_type", "password")
outgoingQueryString.Add("username", "Enter your username")
outgoingQueryString.Add("password", "Enter your password")
outgoingQueryString.Add("client_id", clientid)
outgoingQueryString.Add("client_secret", clientsecret)
outgoingQueryString.Add("content-Length", 53)
outgoingQueryString.Add("Host", "Enter your domain without https://")
Dim postBytes As Byte() = New ASCIIEncoding().GetBytes(outgoingQueryString.ToString())


Dim poststream As Stream = request.GetRequestStream
poststream.Write(postBytes, 0, postBytes.Length)
poststream.Flush()
poststream.Close()

Dim sresponse As String

Using response As WebResponse = request.GetResponse()
Using streamReader As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())
sresponse = streamReader.ReadToEnd
Dim json1 As String = "{'root': " & sresponse & " }"
Dim doc As XmlDocument = JsonConvert.DeserializeXmlNode(json1)
Dim nodes As XmlNodeList = doc.DocumentElement.SelectNodes("/root")
For Each node As XmlNode In nodes

token = node.SelectSingleNode("access_token").InnerText
Next

End Using

End Using

Return token

End Function

Solution
C
Christophe Platinum Contributor
5 years ago
GP
Gareth Perry
5 years ago

Hi,

thank you for the response. i managed to get this working in the end

thanks

Gareth

D
DDMOD@SOTI Platinum Contributor
5 years ago

Hi Gareth Perry,

Thanks for confirming that your issue has been resolved by you. Can you please share the approach you took to find resolution, so that other viewers experiencing the same issue may find 'solution' from your post?

SOTI would be very happy to mark your reply as "solution" for this post.

Thanks & Regards,