'401 Unauthorised' when calling a url with Net.WebClient - vb.net

In my app I'm making a call to an API.
I start by building the URL...
Dim url As String = "http://" & Home.sn_username & ":" & Home.sn_password & "#" & Home.sn_ip & "/command.htm?number=" & phonenumber
url = Replace(url, Chr(13), "")
I want to make sure the URL its attempting to call is correct, so i've dumped it to the console...
Console.WriteLine("DIALING: " & url)
The URL is spot on, so finally I call the URL with Net.Webclient...
Dim client As New Net.WebClient
client.DownloadString(url)
This does not work.
An exception is thrown and I get a 401 Unauthorised error.
Am i doing something wrong?
The final URL is like this:
http://username:passwrd#domainame.com/command.htm?Number=01611234565
If i take that and paste it directly into my browser it works fine! (its for an IP phone API... my desk phone dials the number!).
Do i need to look at a different way of supplying the credentials or something?
Thanks in advance!

Try adding your authentication credentials directly to the WebClient instead of prepending them before the url before executing.
client.Credentials = new NetworkCredential(Home.sn_username, Home.sn_password)

Related

IBM MAXIMO: How to create PO from PR with REST API

in IBM Maximo 7.6.0.8 I would like to create PO from a PR using REST API and VBA (HTTP requests).
My code is:
Dim PostData() As Byte
Dim IE As InternetExplorerMedium
Set IE = New InternetExplorerMedium
PostData = "~date=23-08-2018"
PostData = StrConv(PostData, vbFromUnicode)
myheader = myheader & "x-http-method-override: createPOsFromPR" & vbNewLine
myheader = myheader & "Content-Type: application/json" & vbCrLf
IE.Navigate "https://host:port/maxrest/rest/mbo/pr/123", 0, "myIE", PostData, myheader
But I get Error 500. Authentication is not the problem.
In general I would like to be albe to copy (assign) PR lines to POs and RFQs.
Is this the way to do this? Does someone has working solution?
Thanks!
You could use the New REST API that allows you to directly call an automation script.
You'd add the PR number and date as request parameters and use them in the script to call
the PR.createPOsFromPR method.
Your call would look like this:
http://maximohost/maximo/oslc/script/genPOsScript?prnum=ABC&date=2019-11-21
You might want to add the site as param too because PR is a site level object.
You can see an example here:
https://developer.ibm.com/static/site-id/155/maximodev/restguide/Maximo_Nextgen_REST_API.html#_automation_scripts

VB.Net WebClient DownloadFile Won't Save to Variable Filename?

Trying to download a website to a file using WebClient DownloadFile works fine as long as I have a preset filename for it. If the filename is variable it throws an exception and I can't work out why.
dim link as string = "http://google.com"
Dim myWebClient As New System.Net.WebClient
myWebClient.DownloadFile(link, link & ".html")
If the output is set to "site.html" it'll work just fine but I can't get it to accept a variable.
I'm passing the variable link to the sub and I want it to save that site to The-Sitename.html. I'm guessing there is something really simple I'm missing here?
You cant save a file with the website extension included in the URL
Try using the following code to exclude common extensions to hopefully fix the issue
link = link.Replace("http://", "")
link = link.Replace("https://", "")
myWebClient.DownloadFile(link, link & ".html")

VB Web query request

I am trying to send a query request from VB Login form to Website to check if user Email and Password are Valid.
If I am using request string: http://mathsquest.com/api.php?email=testemail&password=testpass
it returns value "1" as "testemail & "testpass" are pre-registered correct values.
How to send the request with user input values stored in Login form variables (EmailInput and PasswordInput)?
Thank you for help
If I correctly understand your question you should build your url request and then make a redirect or an http request:
Dim req_url as string = "http://mathsquest.com/api.php"
req_url &= "?email=" & EmailInput & "&password=" & PasswordInput
Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString(req_url)
P.S. As suggested in the comments you should consider hashing/crypting your parameters

Google OAuth2 Error 400 when exchanging for an Access Token

I am receiving a "400 Bad Request" error when using the following VBA code to exchange a valid Authorization token for an access token in the Google API. Can anyone shed light as to why, I have been struggling with this one for over a week.
Dim http As MSXML2.XMLHTTP
Dim sUrl As String
Dim sUrlHeader As String
Dim svarbody As String
Set http = New MSXML2.XMLHTTP
sUrl = "https://accounts.google.com/o/oauth2/token? HTTP/1.1"
http.Open "POST", sUrl
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
svarbody = "code=4%2FXAjmXiUlBXUAUGCnMvNKsxdyuJEJ.8kfzzrqo3wwTuJJVnL49Cc9gML_lbgI&" & _
"client_id=50487549202#-q27v28nvhmjhc0uobq35tjn09lhrh47r.apps.googleusercontent.com&" & _
"redirect_uri=http%3A%2F%2Flocalhost&" & _
"scope=https%3A%2F%2Fwww.google.com%2Fcalendar%2Ffeeds&" & _
"client_secret=<secret delete for this post>&" & _
"grant_type=authorization_code"
http.send svarbody
Me.Text3 = http.status & vbCrLf & http.statusText & vbCrLf & http.responseText
this is a very very common problem. You may try to solve it doing many approaches and tests... but...
The really one you should try in the first place is:
UPDATE YOUR SERVER/COMPUTER WINDOWS CLOCK, USE time.nist.gov (or brothers). Double-click in your Windows Hour-Date (on the bottom of your screen and change Internet Time in this way)
SntsDev
In my experience using Oauth 2.0 in VBA this error happens when you use the code more than once for requesting a token. The usage sequence I use based on the documentation and experience is:
access GET https://accounts.google.com/o/oauth2/auth once for geting the code
access POST https://accounts.google.com/o/oauth2/token once for geting the token
after that use the token as long as it is valid
It is possible that you need set up billing info in your google account.

vb.net problems posting to web form

I've hit a wall on POST'ing data to a webform :(
Below is the code I've adapted from many places (here most recently: http://p2p.wrox.com/asp-net-1-0-1-1-professional/34517-webrequest-webresponse-form-submit-problem-help.html )
Dim url As String = "https://student.ashford.edu/student/"
Dim data As String = "username=myusername&password=mypassword"
Dim buffer As Byte() = Encoding.UTF8.GetBytes(data)
Dim result As String = ""
Dim req As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
req.Method = "POST"
req.ContentType = "application/x-www-form-urlencoded"
req.ContentLength = buffer.Length
req.CookieContainer = New CookieContainer()
' enable cookies
Dim reqst As Stream = req.GetRequestStream()
' add form data to request stream
reqst.Write(buffer, 0, buffer.Length)
reqst.Flush()
reqst.Close()
Console.WriteLine(vbLf & "Posting data to " & url)
Dim res As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
' send request,get response
Console.WriteLine(vbLf & "Response stream is: " & vbLf)
Dim resst As Stream = res.GetResponseStream()
' display HTTP response
Dim sr2 As New StreamReader(resst)
result = sr2.ReadToEnd()
Using writer As System.IO.StreamWriter = New StreamWriter("C:\Temp\checkcheck.html")
writer.Write(result)
End Using
When I check the checkcheck.html file, it doesn't show the page I normally see when I've successfully logged into my university, but instead shows the login page again prompting for a user name and password. Since it's prompting for a user/pass in the output response, does that mean nothing is posted? Or is the login button not being hit?
As far as the field names in the source, the name of the username field is "username", and the name of the password field is "password". From what I can tell, the name of the login button itself is "submit". Below is the source of the page, am I missing something?
view-source:https://student.ashford.edu/student/
https://student.ashford.edu/student/
What I want to have happen is that in my VB.NET backend, I click a button and my username and password are automatically entered and I am automagically logged in. Once the login is done, I want to retrieve the HTML of the logged in page so I can parse it for items relating to my studies. This may be unrelated, but do I need cookies to keep my application "logged in" if I navigate to other pages?
EDIT:
I tried plugging different URL's, to no avail. I have also tried adding submit=login and login=submit (alternately of course) to the POST data. I'm still getting the original login screen HTML from the result variable at the end, instead of my university homepage that I'm expecting after I normally login by hand as a human would. Maybe there is a follow up step that I am missing?
The actual form action is at https://student.ashford.edu/login/commands/login.php. You might plug that into url instead. Also, try including the submit button - submit=login - in the values.
I found out what I needed to do. First I have to make sure that I have a cookie container. Next, I used Fiddler to find out EXACTLY what is being sent back and forth between my browser and where I want to login. I post all the login info, and I get the response back. This seems to fill up my cookie container variable with the required authentication cookies? Next I make another POST request to the page I want to go to, and voila - it works.