Unable to create a cURL string in VB.net - vb.net

I I'm trying to integrate an SMS service in my desktop application in VB.net but I'm not able to create the correct string to to that.
This is the autentication method:
Session Key example
curl -XGET 'https://app.esendex.it/API/v1.0/REST/login' -H 'Content-Type: application/json'
-H 'Authorization: Basic base64_encode(MY_USERNAME:MY_PASSWORD)'
On success, the above command returns the following response: USER_KEY;SESSION_KEY
This is what I've done:
Private Sub SMS()
Dim username = "myuser"
Dim password = "mypass"
Dim encoded As String
encoded = System.Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password))
Dim webRequest As WebRequest = WebRequest.Create("https://app.esendex.it/API/v1.0/REST/login")
webRequest.ContentType = "application/json"
webRequest.Headers.Add("Authorization", "Basic " + encoded)
webRequest.Method = "GET"
Dim myHttpWebResponse = CType(webRequest.GetResponse(), HttpWebResponse)
TextBox1.Text = myHttpWebResponse.ToString
End Sub
but myHttpWebResponse.ToString doesn't get USER_KEY & SESSION_KEY
Can someone help me ?

Related

Curl URL into vb.net

Hi i'm trying to find a solution how to implement the below curl in vb.net:
curl -X 'GET' \ 'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd&include_market_cap=false&include_24hr_vol=false&include_24hr_change=false&include_last_updated_at=false&precision=full' \ -H 'accept: application/json'
Using System.Net.Http you can do that as follows:
Private Sub GetBtcPrice()
Using client As System.Net.Http.HttpClient = New System.Net.Http.HttpClient
client.DefaultRequestHeaders.Add("accept", "application/json")
Dim sb As New StringBuilder
sb.Append("?")
sb.Append("ids=bitcoin&")
sb.Append("vs_currencies=usd&")
sb.Append("include_market_cap=false&")
sb.Append("include_24hr_vol=false&")
sb.Append("include_24hr_change=false&")
sb.Append("include_last_updated_at=false&")
sb.Append("precision=full")
Dim response As HttpResponseMessage = client.GetAsync("https://api.coingecko.com/api/v3/simple/price" & sb.ToString).GetAwaiter().GetResult()
Dim sResponse As String = response.Content.ReadAsStringAsync().GetAwaiter().GetResult()
Console.WriteLine("response: " & sResponse)
End Using
End Sub
Dim webClient As New System.Net.WebClient
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
Dim result As String = webClient.DownloadString("https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd&include_market_cap=false&include_24hr_vol=false&include_24hr_change=false&include_last_updated_at=false&precision=full")
And its stored in variable result as json

Correct way to add "autorization : bearer" header in an HttpWebRequest

Context
I want to code this curl command in VB.NET
curl -X GET 'https://api.twitch.tv/helix/users?login=crunchprank' \
-H 'Authorization: Bearer YY' \
-H 'Client-Id: XX'
This curl works perfectly if executed in online curl like https://reqbin.com/curl
Client Id and Bearer are ok.
Here is my VB.Net code
Dim strTwitchApiRequestChannelUrl As String
Dim httpTwitchApiRequestChannel As HttpWebRequest
Dim httpTwitchApiRequestChannelResponse As HttpWebResponse
Dim strTwitchApiRequestChannelResponse As String
strTwitchApiRequestChannelUrl = "https://api.twitch.tv/helix/users?" & strTwitchChannelId
httpTwitchApiRequestChannel = CType(WebRequest.Create(strTwitchApiRequestChannelUrl), HttpWebRequest)
httpTwitchApiRequestChannel.Timeout = 10000
httpTwitchApiRequestChannel.Headers("client_id") = "XX"
httpTwitchApiRequestChannel.Headers("Authorization") = " Bearer " & YY
httpTwitchApiRequestChannelResponse = TryCast(httpTwitchApiRequestChannel.GetResponse(), HttpWebResponse)
If httpTwitchApiRequestChannelResponse.StatusCode = 200 Then
Using responseStream As Stream = httpTwitchApiRequestChannelResponse.GetResponseStream()
Dim reader As StreamReader = New StreamReader(responseStream)
strTwitchApiRequestChannelResponse = reader.ReadToEnd()
End Using
End If
Issue:
I get an 401 response code. I'm pretty sure it's because of the autorization headers. It missed the :
Same issue if i did this :
httpTwitchApiRequestChannel.Headers("Authorization") = ": Bearer " & YY
And I cannot do this httpTwitchApiRequestChannel.Headers("Authorization: Bearer ") = YY because i got an error: Specified value has invalid HTTP Header characters. (Parameter 'name')"}

converting curl -x get to vb.net without success

I need to convert this to vb.net:
curl -X GET --header 'Accept: application/json' --header 'X-API-KEY: xxxxxxxxxxxxxxxx' 'https://app.atera.com/api/v3/agents'
What I've tried is:
Dim myHttpWebRequest = CType(WebRequest.Create("https://app.atera.com/api/v3/agents"), HttpWebRequest)
myHttpWebRequest.Headers.Add("Accept", "application/json")
myHttpWebRequest.Headers.Add("X-API-KEY", "XXXXXXXXXXXXXXX")
Dim myHttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
TextBox1.Text = myHttpWebResponse.ToString
Unfortunatly whatever I tried I get an error.
Can you help me out here?
Got it worked.
They new code that works is:
Dim myHttpWebRequest = CType(WebRequest.Create("https://app.atera.com/api/v3/agents/130"), HttpWebRequest)
myHttpWebRequest.Accept = "application/json"
'myHttpWebRequest.Headers.Add("Accept", "application/json")
myHttpWebRequest.Headers.Add("X-API-KEY", "XXXXXXXXXXXX")
Dim myHttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
Dim responseReader As StreamReader = New StreamReader(myHttpWebResponse.GetResponseStream)
Dim result As String = responseReader.ReadToEnd()
TextBox1.Text = result
End Sub

VB.NET 401 Unauthorized error in PayPal OAuth API

I'm trying to get an access token back from PayPals OAuth API, I can get it back with curl but need to get the code through vb.NET. I always get a 401 unauthorized response but can't see why. I'm using the following code:
Dim uri as new Uri("https://api-m.sandbox.paypal.com/v1/oauth2/token")
Dim wClientID As String = "client ID"
Dim wClientSecret As String = "Client Secret"
Dim data as string = "grant_type=client_credentials"
Dim dataByte as Byte()
ServicePointManager.Expect100Continue = true
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
request = WebRequest.Create(uri)
request.Headers.Add("Username", wClientID)
request.Headers.Add("Password", wClientSecret)
request.Headers.Add(HttpRequestHeader.Authorization, "Basic " & wClientID & ":" & wClientSecret)
request.ContentType = "application/x-www-form-urlencoded"
request.Method = "POST"
dataByte = Encoding.UTF8.GetBytes(data)
Using requestStream = request.GetRequestStream
requestStream.Write(dataByte, 0, dataByte.Length)
requestStream.Close()
End Using
Any help would be appreciated, I've triple checked the client id and secret, both are definitely correct
You need to Base64 encode the clientid:secret string for HTTP basic authentication. Curl does this for you.

VB.NET Rest Client POST -d body

I have a Rest API that i can query normal. Unfortunately I have to submit parameter values as body content.
CURL code is like
curl -X POST "https://URL" -H "accept: application/json" -H "authorization: bearer [BEARER GOES HERE] -H "Content-Type: application/json" -d "{ \"ids\": [\"ldt:b3b6ea07a6b848fdb5ca3e36bf1e3678:21475014799\",\"ldt:b3b6ea07a6b848fdb5ca3e36bf1e3678:8590616500\"]}"
I'm able to query the API like this:
Dim request = New RestRequest("/detects/queries/detects/v1, Method.POST)
request.AddHeader("Accept", "application/json")
request.AddHeader("Authorization", "bearer " + bearer)
data_response = data_client.Execute(request)
data_response_raw = data_response.Content
The thing that I'm struggling with is to add the body / -d part of the curl request. The content is fairly simple:
{"ids":["ldt:b3b6ea07a6b848fdb5ca3e36bf1e3678:21475014799"]}
I tried request.AddBody(Of RequestBody)(body) but that gives me only a syntax error.
Can anybody give me a hint on this?
Thank you!
I have personally never used RestSharp, however here is the documentation related to specifying the request body of the RestRequest object: https://restsharp.dev/usage/parameters.html#request-body
Specifically, the documentation suggests using the AddJsonBody method: https://restsharp.dev/usage/parameters.html#addjsonbody
Here is an example using your desired body content:
Dim param = New With {Key .ids = {"ldt:b3b6ea07a6b848fdb5ca3e36bf1e3678:21475014799"}}
request.AddJsonBody(param)
Davids hint did the trick - this is the code working
Dim sb0 As New StringBuilder
sb0.Append(Chr(123) + Chr(34) + "ids" + Chr(34) + Chr(58) + Chr(91))
Dim append As String
Dim zahl = 0
Do Until zahl = nmbr2 - 1
append = raw_d("resources")(zahl)
sb0.Append(Chr(34) + append + Chr(34) + ",")
zahl = zahl + 1
Loop
append = raw_d("resources")(zahl)
sb0.Append(Chr(34) + append + Chr(34))
sb0.Append(Chr(93) + Chr(125))
Dim body As String = sb0.ToString
Dim request = New RestRequest("xxx", Method.POST)
request.AddHeader("Accept", "application/json")
request.AddHeader("Authorization", "bearer " + bearer)
request.AddJsonBody(body)
data_response = data_client.Execute(request)
data_response_raw = data_response.Content
Maybe not the prettiest way to create the body but it works ;)