Xmlhttp request is raising an Access Denied error - vba

The following Excel macro, which is making an xmlhttp request to this webpage to retrieve some values at a second stage, has worked normally in VBA until some time ago:
Sub WebReq()
Link = "http://it.finance.yahoo.com/q?s=^FCHI&ql=10" & str(rnd())
Set htm = CreateObject("htmlFile")
Set RequestWeb = CreateObject("msxml2.xmlhttp")
With RequestWeb
.Open "GET", "" & Link & "", False
.send
htm.body.innerhtml = .responsetext
End With
End Sub
Now, instead, at the call of the method:
.send
of the object msxml2.xmlhttp is raising the following error:
Run-time error '-2147024891 (80070005)'
Access is denied.
I've been looking on the web but all the similar threads are never answered. Can anyone explain me what this error means, and if there's any way I could fix it or even just work around it?
Note: the random string at the end of the variable 'Link' has been added to force the page reloading, since the script is retrieving real-time values and so it should be loaded every time.
Additional information: while looking for a solution, I'm noticing now that the random part of the link is yielding always the same value even when I end the running and restart again:
Link = http://it.finance.yahoo.com/q?s=^FCHI&ql=10 .7055475
Why is this happening? Shouldn't rnd() yield a new random value between 0 and 1 at every call?

Use
CreateObject("MSXML2.ServerXMLHTTP.6.0")
The standard request fired from a local machine forbids access to sites that aren't trusted by IE. MSXML2.ServerXMLHTTP.6.0 is the server-side object, that doesn't perform those checks.

i found that, in my case, changing http to https fixed the access denied problem. i can only assume that the website somehow made a change and didn't tell anyone

access denied is IE issue
internet options > security tab > custom security level > Miscellaneous >Access data sources across domains > enable

Update
Sub WebReq()
link = "http://it.finance.yahoo.com/q?s=^FCHI&ql=10" & Str(Rnd())
Set htm = CreateObject("htmlFile")
Dim objHttp
Set objHttp = CreateObject("Msxml2.ServerXMLHTTP")
objHttp.Open "GET", link, False
objHttp.Send
htm.body.innerhtml = objHttp.responsetext
Set objHttp = Nothing
End Sub

This works for me:
With CreateObject("MSXML2.ServerXMLHTTP.6.0")
.Open "GET", URL, False
.Send
content = .ResponseText
End With

In my case the user didn't have AD permissions to the proxy server on our corporate network. (Simple oversight when setting up the user.) Adding the missing security group fixed the problem for the user.

I was able to fix it by changing the link being passed from being "http://" to "https://"
The site I was pulling had upgraded and trying to pull the data using the unsecured link was failing. Works great now (no code change required.

add "www" after "https://" in your custom Link,
Like this:
XMLPage.Open "GET", "https://www.x-rates.com/table/?from=GBP&amount=3", False
XMLPage.send

I'm afraid I don't understand exactly why this problem occurs, but I'm guessing it is the secure "https://" versus insecure "http://". I ran into the same "access denied" message while following sample code from a VBA course. The original code was:
XMLPage.Open "GET", "http://x-rates.com/table/?from=GBP&amount=3", False
XMLPage.send
I changed the "http://" to "https://" and the error went away.

Related

Do SOAP calls in VBA use IE by default?

I'm trying to find a way around using IE in my VBA code to access my portfolios in finance.yahoo.com but I'm getting the same message using a soap/rest? format as I do when I open an IE window: "Some parts of this page is not supported on your current browser version. Please upgrade the browser to the latest." It looks like my new calls are being processed by IE also. Is that true? Is there a way around this?
I tried using Selenium to open Firefox but it isn't working with the current version of Firefox (67)
here's the code I'm using now:
Dim xmlhttp As new MSMXML2.XMLHTTP60
Dim WebPgDoc As New HTMLDocument
Dim objNode As object
With xmlhttp
.Open "GET", "https://finance.yahoo.com/portfolio/pf_14/view/view_18", False
.send
WebPgDoc.body.innerHTML = .responseText
End With
Set objNode = WebPgDoc.getElementsByTagName("div")
When I look at objNode(70).innertext it has the same error message and missing stock details as when I use code to open IE to open the portfolio .
Any ideas would be greatly appreciated.
Thanks

Why do i get "Argument is not optional" error on simple url call?

My goal is to check the internet connecton in VBA but when i try to call a url with request.open I get the error message "Argument is not optional".
Sadly internet research has no yielded any results.
This is my code:
On Error GoTo NoConnectionErrorHandling
Dim Request As MSXML2.XMLHTTP60
Request.Open "http://www.google.com"
Request.send
MsgBox Request.Status
It hangs itself in the third line of the shown code.
I hope someone can help me as i have very very little experience in VBA yet.
You have to specify the type of the request. It can be "GET", "POST" or something else.
See here for the open Method:
https://msdn.microsoft.com/en-us/library/ms757849(v=vs.85).aspx
These are the parameters:
bstrMethod
The HTTP method used to open the connection, such as GET, POST, PUT, or PROPFIND. For XMLHTTP, this parameter is not case-sensitive. The verbs TRACE and TRACK are not allowed when IXMLHTTPRequest is hosted in the browser. What is the difference between POST and GET?
bstrUrl
The requested URL. This can be either an absolute URL, such as "http://Myserver/Mypath/Myfile.asp", or a relative URL, such as "../MyPath/MyFile.asp".
varAsync[optional]
bstrUser[optional]
bstrPassword[optional]
As you see, the method needs at least two parameters (the other 3 are optional) thus 1 is not enough.
You need to declare Request with the keyword New. Thus, something like the following piece works:
Public Sub TestMe()
Dim Request As New MSXML2.XMLHTTP60
Request.Open "GET", "http://www.bbc.com"
Request.send
MsgBox Request.Status
End Sub
Whenever you are using libraries outside the standard VBA libraries it is a good idea to do one of the following 2:
Provide information for the library:
Use late binding:
Dim Request As Object
Set Request = CreateObject("Msxml2.ServerXMLHTTP.6.0")

VBA GET request failing, bad format?

I'm a novice and your help has been amazing so far! I have an issue with a GET request for SSO authentication in an Excel Visual Basic script. The API says to send the GET request to:
https://services.example.com/API/Security.svc/SSOSiteLogin/siteId/enterpriseGuid/authToken/timezoneOffset
Where the values for siteID, enterpriseGUID, authToken, and timezoneOffset have all been obtained by previous GET requests, which have worked fine. When I send the final GET request for a security token, it returns:
{"Detail":"SSOSiteLogin failed.Input string was not in a correct format."}
Here is my relevant code:
Dim hreq As New WinHttpRequest
Dim response As String
Dim URL As String
'get security token
URL = "https://services.example.com/API/Security.svc/SSOSiteLogin/siteID/enterpriseGUID/" & authToken & "/null"
hreq.Open "GET", URL, False
hreq.Send
response = hreq.ResponseText
Range("A13") = response
The siteID and enterpriseGUID variables are input into the code, and the authToken is stored from an earlier POST request. The last value, timezoneOffset, is given as "null" from an earlier request so I've been typing "/null" at the end but I'm concerned that's what's triggering the incorrect format response. Anything else just returns a total error from the server though.
Any ideas? Thank you in advance!
I had put the values into cells and was sending the GET request as "Range("E5") & "/" & Range("E6") & "/", etc. so that I could try a couple of debugging issues without having to go to the VBA editor and for some reason that worked! I ended up using the string "null", too. If anyone knows why this worked and inputting it all as one string doesn't I'd be interested to find out.

Use live feeds (RSS?) to convert USD to GBP in VB form

I've searched the web and can't find the exact thing that I'm looking for - this could be because it doesn't exist, but I'll ask here anyway...
I want to use a pretty simple from written using VB, I've worked with things like RSS feeds before, but only in a HTML environment. I was wondering if there is a way to have a section of the form as a USD -> GBP converter, using a live exchange rate. Is this possible? If so, does anyone know a good source to get the live feed from?
Any ideas, code, suggestions and criticism is welcome.
Thanks for your time.
Cal.
The simplest way is to query the Yahoo currency converter using this function:
Public Function GetHTML(ByVal sURL As String) As String
'Function returns the contents of the web page at sURL
'(or the contents of the 404/error info, if sURL is
'invalid, incorrectly formatted, etc.)
Dim xmlHttp As Object
Set xmlHttp = CreateObject("MSXML2.ServerXmlHttp")
xmlHttp.Open "GET", sURL, False
xmlHttp.send
GetHTML = xmlHttp.responseText
Set xmlHttp = Nothing
End Function
Notice that it's using ServerXmlHttprather than just XmlHttp. This is because the latter was returning an Access denied error when I tried it, I'm not sure why.
Call it from your form code using something like this:
lblRate.caption = GetHTML("http://finance.yahoo.com/d/quotes.csv?&f=l1&s=USDGBP=X")
Note:
AFAICT there isn't any official documentation on the Yahoo API. But there are plenty of examples of it's usage if you search online.

ASPTwitter library fails when using special characters

I am trying to update an old ASP classic Twitter program that my work currently uses to use the new OAUTH. I am not an ASP programmer but I managed to find the ASPTwitter library posted online by Tim Acheson at http://www.timacheson.com/Blog/2013/jun/asptwitter
Everything works, as we have our own code searching our database and passing on a built string to the ASPTwitter code to tweet.
The catch is that it will fail with the
{"errors":[{"message":"Could not authenticate you","code":32}]}
error message if there is so much as a "." period in the string. Every possible special character besides letters and numbers causes a fail.
We have many posts that will include various symbols as well as URLs.
I have searched all over and have not been able to find a solution. Comments on Tim's site have mentioned it but no solutions yet. Everyone here has been very helpful so I was hoping someone might have a solution.
I can't post the code as there are about 6 files and I don't know which one is causing the issue.
Thank you so much for the help!
Edit:
This is a block of the 300+ line file where the issue happens, I hope that the cause can be found here too.
' Gets bearer token for application-only authentication from Twitter API 1.1.
' Application-user authentication: https://dev.twitter.com/docs/auth/using-oauth
' and: https://dev.twitter.com/docs/auth/authorizing-request
' API endpoint statuses/update (post a tweet): https://dev.twitter.com/docs/api/1.1/post/statuses/update
Private Function UpdateStatusJSON(sStatus)
Dim sURL : sURL = API_BASE_URL + "/1.1/statuses/update.json"
Dim oXmlHttp: Set oXmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
oXmlHttp.open "POST", sURL, False
sStatus = "this is from the ASPTwitter dot asp file"
oXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"
oXmlHttp.setRequestHeader "User-Agent", "emiratesjobpost.com"
oXmlHttp.setRequestHeader "Authorization", "OAuth " & GetOAuthHeader(sURL, sStatus)
oXmlHttp.send "status=" & Server.URLEncode(sStatus) ' Encoded spaces as + in request body.
UpdateStatusJSON = oXmlHttp.responseText
Set oXmlHttp = Nothing
REM: A JSON viewer can be useful here: http://www.jsoneditoronline.org/
' To fix error message "Read-only application cannot POST" go to your application's "Application Type" settings at dev.twitter.com/apps and set "Access" to "Read and Write".
' After changing access to read/write you must click the button to generate new auth tokens and then use those.
Response.Write "<textarea cols=""100"" rows=""3"" >" & UpdateStatusJSON & "</textarea>" : Response.Flush()
End Function
If I replace the "dot" with "." in the "sStatus" line, it breaks
Do your pages use UTF-8 encoding? Open them in Notepad, select save as from the file menu, and if ansi coding is selected then change it to UTF-8.
See this link for more things you can do
http://www.hanselman.com/blog/InternationalizationAndClassicASP.aspx