1

I'm having difficulties with an API (Alsi, this is the documentation: https://alsi.gie.eu/GIE_API_documentation_v003.pdf) that provides natural gas data. It allows me to put a URL into my internet browser so I can see the Json-formatted data I need. However, if I wanted to download that Json, I should use a "x-key" they also gave me with a cURL command, like this one that's given in the documentation as an example:

"https://agsi.gie.eu/api/data/eu?from=2017-06-01&till=2017-06-14" --header "x-key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  

Since we want to automatize some processes, the idea would be to have a macro-enabled Excel doc. that would retrieve this information easily so it could be used in Excel. Thanks to other open questions here in StackOverflow I have the following VBA code that seems to interact with the API, however it returns "access denied", and my best guess is that this happens because the password (or "x-key") isn't been inputted correctly. This is all I have (aside of JsonConverter and Base64Encode in other modules):

Sub test()

 Dim httpObject As Object
 Set httpObject = CreateObject("MSXML2.XMLHTTP")

 sUrl = "https://alsi.gie.eu/api/data/eu?from=2018-11-15&till=2018-12-04"
 sAuth = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"


 sRequest = sUrl
 httpObject.Open "GET", sRequest, True
 httpObject.setRequestHeader "x-key", Base64Encode(sAuth)
 httpObject.send
 sGetResult = httpObject.responseText

 MsgBox sGetResult

 Dim oJSON As Object
 Set oJSON = JsonConverter.ParseJson(sGetResult)

 For Each sItem In oJSON
  dItemDate = oJSON(sItem)("date")
  sItemString = oJSON(sItem)("string")
  vItemValue = oJSON(sItem)("value")

  MsgBox "Item: " & sItem & " Date: " & dItemDate & " String: " & sItemString & " Value: " & vItemValue
 Next

End Sub

I tried also appliying Base64Encode to the password variable (sAuth) as you can see in case that was the problem, also without it, also without a password/key at all... but I always get the same "access denied" message, which is the reason why I think the password is the issue. At this point I really don't know how to make any progress, this is the closest I got to obtaining data from this API in Excel via VBA (I tried with many more chunks of code here and in other websites/forums before) and I feel I'm very close, just not there yet.

Thanks a lot in advance.

1 Answer 1

1

You don't need to encode the key. Not sure why that attempt didn't work for you without though make sure you pass False in the .Open line.

I note that the response is a collection of dictionaries. Those dictionaries do not have the keys you specify in your script.

Option Explicit
Public Sub GetJSON()
    Dim json As Object   
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://agsi.gie.eu/api/data/eu?from=2017-06-01&till=2017-06-14", False
        .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
        .setRequestHeader "x-key", "APIkey"
        .send
        Set json = JsonConverter.ParseJson(.responseText)
    End With
End Sub

JSON sample:

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply, I think I know what you mean, stupid mistake from me, I'll keep looking from this point on, thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.