EXCEL VBA中CURL的令牌身份验证

时间:2019-05-29 15:20:08

标签: vba authentication curl response

一个客户要求我组装一个工具来获取公司域名,然后以某种方式提出帐户名称。我找到了可以使用的API,但是我对在VBA中执行此操作并不熟悉。

这是站点文档提供的CURL过程,用于推送域名,并接收公司名称作为响应:

curl 'https://company.clearbit.com/v2/companies/find?domain=segment.com' \
        -u sk_b3b05a8924c4f3df86248b4e38421cfa:

在尝试几次接收“请验证您的请求”之后,我终于到了从API接收到错误的地步,我认为这表明我现在正在以某种方式正确地调用API。 / p>

这是我当前的代码。知道为什么我会收到此错误吗?

{"error":{"type":"api_error","message":"Sorry, something went wrong. We have been notified."}}
Public Function CallRestAPI2(strUrl1 As String)
    TargetURL = "https://company.clearbit.com/v2/companies/find?domain=toplinegroup.com"
    Set HTTPReq = CreateObject("WinHttp.WinHttpRequest.5.1")
    'HTTPReq.Option(4) = 13056 '
    HTTPReq.Open "GET", TargetURL, False
    HTTPReq.SetRequestHeader "Content-Type", "application/json"
    HTTPReq.SetRequestHeader "Accept", "application/json"
    HTTPReq.Send "-u sk_b3b05a8924c4f3df86248b4e38421cfa"
    Debug.Print HTTPReq.responseText
End Function

知道我在做什么错吗?

1 个答案:

答案 0 :(得分:0)

在对文档进行了更多阅读之后,发现了这一点,这是由于未正确传递Authentication标头。这是我的代码:

Public Function CallRestAPI3(strUrl1 As String)
    Dim xmlhttp As New MSXML2.XMLHTTP60, myurl As String
    myurl = "https://company.clearbit.com/v2/companies/find?domain=toplinegroup.com"
    xmlhttp.Open "GET", myurl, False
    xmlhttp.setRequestHeader "Authorization", "Bearer " + "sk_b3b05a8924c4f3df86248b4e38421cfa"
    xmlhttp.Send
    Debug.Print (xmlhttp.responseText)
End Function