Excel VBA: Get Yahoo Finance data - excel

I want to build a simple spread sheet that make use of latest stock quotes from Yahoo, I tried a few samples from the web, but all failed with this error: "A connection with the server could not be established". Any idea?
Sub getQuotes2()
Dim URL As String
URL = "http://finance.yahoo.com/d/quotes.csv?s=^GSPC&f=l1"
MsgBox URL
Set Http = CreateObject("WinHttp.WinHttpRequest.5.1")
Http.Open "GET", URL, False
Http.send
Dim Resp As String: Resp = Http.responseText
MsgBox Resp
End Sub

Related

Excel VBA API Request

So I don't have any experience coding whatsoever but I'm working on pulling some vehicle data from an government site with an API using VBA. so i don't have to manually adjust data in our vehicle list.
the code i wrote/stole:
Sub SendAPIRequest()
Dim httpreq As Object
Dim url As String
Dim response As String
Dim headers As Collection
Set headers = New Collection
headers.Add "SVV-Authorization", "Apikey {1234}"
Set httpreq = CreateObject("MSXML2.XMLHTTP")
url = "https://www.vegvesen.no/ws/no/vegvesen/kjoretoy/felles/datautlevering/enkeltoppslag/kjoretoydata?kjennemerke=AA91620" + kjennemerke
With httpreq
.Open "GET", url, False
.setRequestHeader "SVV-Authorization", "1234}"
.send
End With
response = httpreq.responseText
Debug.Print response
End Sub
the request goes out but the with the following response: "status":403,"error":"Forbidden","path":"/enkeltoppslag/kjoretoydata"}
403 The API key does not exist in the database, has the status active and/or the user is blocked.
Additional info: REST service - Json response.
https://autosys-kjoretoy-api.atlas.vegvesen.no/api-ui/index-enkeltoppslag.html
key is legit so I'm sending the header out wrong?
Thanks in advance :)
copy & paste other code, etc.

Excel show image from http response

The goal is to have a sheet in excel that contains fields and a button. The click on button will create url from the fields and create a HTTP GET request that returns an image in raw data. This image then should be shown in the current sheet.
This is what i have already done:
Sub GetQrCode()
Dim hReq As Object
Dim ws As Worksheet
Set ws = Sheet1
Dim strUrl As String
strUrl = "https://www.profit365.eu/services/API/QR/PayBySquare.png?IBAN=SK2311000000001234567890&BIC=TATRSKBX&Currency=EUR&Amount=123.40&DueDate=41763&VS=1234568&SS=&CS=&size=256"
Set hReq = CreateObject("MSXML2.XMLHTTP")
With hReq
.Open "GET", strUrl, False
.Send
End With
Dim resp As String
resp = hReq.responseBody
End Sub
Now should come the code that shows the resp (raw image).
Is this possible? Has anyone done something similar?
Thank you for the responses

vba run time error '- 2146697208 800c0008 )': the download of the specified resource has failed telegram

Request your help to solve the below issue,I am struck for two days,
vba run time error '- 2146697208 (800c0008 )': The download of the specified resource has failed
This is my code for your reference:
Sub telebot15status155555()
'Program Excel Messeger
' Purpose : Send Message to Telegram
' Author : John Eswin Nizar
' Date : 09 October 2017
Dim objRequest As Object
Dim strChatId As String
Dim strMessage1 As String
Dim strMessage2 As String
Dim strPostData As String
Dim strResponse As String
strChatId = "#messstatus"
strMessage1 = ""
strMessage2 = Now()
'strMessage = "hallo"
strPostData = "chat_id=" & strChatId & "&text=" & strMessage1 & strMessage2
Set objRequest = CreateObject("MSXML2.XMLHTTP")
With objRequest
.Open "POST", "https://api.telegram.org/sendMessage?", False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.send (strPostData)
GetSessionId = .responseText
MsgBox GetSessionId
End With
End Sub
My advice here is to perform this API call in Chrome with DevTools open. Or access the page, where this happens, and find the Request on the 'Network' tab. Then look at the headers.
debug.print strPostData is returning:
chat_id=#messstatus&text=2/7/2020 9:40:23 AM
That format does not look correct for an API that is form-urlencoded. You need to see what the website is actually sending and receiving in the DevTools in order to replicate it.
IOW, The resource isn't found because the API is incorrectly formatted. From the base api, it looks like its actually a QueryString too because it ends with ? which might mean you have to url-encode format the POST Data into the QueryString for the main HTTP request. Have a look here: Pass Parameters in VBA HTTP Post Request

Which format of the url is correct while using an api with xml

When I try to access common/example api url with vba (excel), there is no problem.
The required URL is as follows: "hppts://user:password#www.site.com/events/"
When I copy the url in web-browser there is no problem.
But when using VBA is returning an error: "Method of Open object IServerXMLHTTPRequest2 has failed"
Code:
Dim http As Object, str As Variant
Set http = CreateObject("MSXML2.XMLHTTP")
With http
.Open "GET", "https://user:password#www.site.com/events/", False
.send
str = .responseText
End With
x = UBound(str)

Podio items to excel sheet

I need to have a vba macro that downloads data from my Podio app to an excel sheet. Right now I am running a code I found on Podio community website, which I am pasting below:
Dim winHttpReq As Object
Dim access_token As String
Function ProjectsFromPodio()
Dim result As String
Dim postData As String
Dim myURL As String
Dim token_pos As Long
'Initialisierung
Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
myURL = "https://api.podio.com/oauth/token"
postData = "grant_type=app&app_id=ABC&app_token=DEF&client_id=GHI&client_secret=JKL"
If winHttpReq.Open("POST", myURL, False) = S_OK Then
'MsgBox ("Open successfull")
End If
winHttpReq.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
winHttpReq.Send (postData)
result = winHttpReq.responseText
token_pos = InStr(result, "access_token")
access_token = Mid(result, token_pos + 15)
token_pos = InStr(access_token, Chr(34)) - 1
access_token = Mid(access_token, 1, token_pos)
OAuthAppAuthorization = result
'MsgBox (OAuthAppAuthorization)
Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
myURL = "https://api.podio.com/item/app/XYZ"
winHttpReq.Open "GET", myURL, False
winHttpReq.SetRequestHeader "Authorization", "OAuth2 " & access_token
winHttpReq.Send
result = winHttpReq.responseText
'MsgBox (result)
End Function
This works nice, data is downloaded and I can use it. My question is how to download this data in CSV format, rather than JSON? Is it possible?
If it is not possible, how can I parse it to a sheet in a smart way?
Thanks for any help
You may send the POST request to /item/app/{app_id}/export/{exporter}which creates a batch for exporting the items.
Here you can export the items as xls and xlsx formats.
Your myURL must be like,
myURL = "https://api.podio.com/item/app/XYZ/export/xls"
You will get a batch_id as a result of this request.
Then make a GET call to /batch/{batch_id} to get the status of the batch. You can download the output xls file by making a GET request to /file/{file_id} using the podio_file_id from the batch response.
References
Podio - Export Items
Podio - Get Batch
Podio - Get File

Resources