How to pass API key in VBA for Get and Post Request?

后端 未结 2 1146
挽巷
挽巷 2021-01-16 23:39

I am using https://developer.companieshouse.gov.uk/api/docs/ to access an API I have my API key, however, i am not sure how to pass that from VBA. So far i tried below

相关标签:
2条回答
  • 2021-01-17 00:18

    I found it.. i was missing to encode the key

    Function EncodeBase64(text As String) As String
      Dim arrData() As Byte
      arrData = StrConv(text, vbFromUnicode)
    
      Dim objXML As MSXML2.DOMDocument
      Dim objNode As MSXML2.IXMLDOMElement
    
      Set objXML = New MSXML2.DOMDocument
      Set objNode = objXML.createElement("b64")
    
      objNode.DataType = "bin.base64"
      objNode.nodeTypedValue = arrData
      EncodeBase64 = objNode.text
    
      Set objNode = Nothing
      Set objXML = Nothing
    End Function
    
    0 讨论(0)
  • 2021-01-17 00:24

    Basic auth requires the username and password to be base 64 encoded together. The AuthKey you need to pass is essentially:

    def unencoded_auth = "[username]:[authkey]"
    def encoded_auth = *call-to-base64-encode-value*(unencoded_auth)
    

    Then you would replace

    "Basic " & AuthKey
    

    with

    "Basic " & encoded_auth
    

    I'll reference this post as to how to achieve the base64 encoding.

    0 讨论(0)
提交回复
热议问题