如何在VBA中为Get和Post请求传递API密钥? [英] How to pass API key in VBA for Get and Post Request?

查看:174
本文介绍了如何在VBA中为Get和Post请求传递API密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 https://developer.companieshouse.gov.uk/api/docs/访问API 我有我的API密钥,但是,我不确定如何从VBA传递它.到目前为止,我在下面尝试过

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

AuthKey = [Key received]

With CreateObject("Microsoft.XMLHTTP")
        .Open "GET", strUrl, False, authKey
        .SetRequestHeader "Content-Type", "application/json"
        .SetRequestHeader "Accept", "application/json"
        .SetRequestHeader "Authorization", "Basic " & AuthKey
        .Send
        response = .ResponseText
    End With

当我从其测试页尝试 https://developer.companieshouse.gov.uk/document/docs/document/id/content/fetchDocument.html 效果很好,当我转到授权密钥不同的开发工具时,我认为我缺少一些编码.有人可以帮忙吗

When i try from their test page https://developer.companieshouse.gov.uk/document/docs/document/id/content/fetchDocument.html it works well and when i goto dev tools that Authorization key is different, i think i am missing some encoding. Can someone please help

谢谢

推荐答案

我找到了它.我不知道要对密钥进行编码

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

这篇关于如何在VBA中为Get和Post请求传递API密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆