将有效的Python API脚本转换为VBA-

时间:2018-07-20 20:09:53

标签: python excel vba api winhttprequest

我已经搜寻了几天的互联网,现在正在寻找将我的API脚本转换为VBA以使其可以直接在Excel文件中运行的解决方案。我设法复制了在我的python脚本中生成的签名密钥。但是,我仍然遇到无效的凭证错误,因为我相信我以正确的方式构造了标头和参数。

HEADER_A,HEADER_B和HEADER_C是文档中概述的所需标题的占位符。参数“ job_id”也是必需的。

有效的python脚本:

signature = generateSignature(sigtag,key)
headers = {"HEADER_A": signature,
           "HEADER_B": timestamp,
           "HEADER_C": employee_id}
apirequest = requests.get(GEOPAL_BASE_URL + apicall,
                          headers=headers,
                          params={"job_id": job_id})
apifeedback = apirequest.json() 

我一直在拼凑解决问题的方法,但是我找不到一个例子,其中有人不得不同时使用参数和标头,就像我必须与上面的请求库一起使用。

到目前为止,这是我在VBA中的进步

Private Sub cmdgetjobinfo()
    Dim URL As String, timestamp As String, empID As String, key As String, method As String, signature As String, hash As String, params As String
    Dim objHTTP As Object: Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    Dim ws As Worksheet: Set ws = Worksheets("API")        
    URL = "https://app.addressofwebsite.com/"
    uri = "api/jobs/get"
    timestamp = Format(Now(), "ddd, DD MMM YYYY HH:MM:SS") & " +0000"
    empID = "715"
    method = "get"
    key = "abckey"

    hash = ComputeHMACSHA256(key, method & uri & empID & timestamp)
    signature = EncodeBase64(hash)
    params = ("job_id=testid")

    objHTTP.Open "GET", URL & uri, False
    objHTTP.setRequestHeader "Content-Type", "application/json", "Accept", "application/json"
    objHTTP.setRequestHeader "Authorization", "HEADER_A", signature,"HEADER_B", timestamp,"HEADER_C", empID
    objHTTP.send params
    strResult = objHTTP.responseText
    Worksheets("API").Range("A10:A10") = strResult
End Sub

脚本的当前输出:

{"status":false,"tag":"api\/jobs","error_code":0,"error_message":"Invalid Credentials"}

我知道问题围绕'objHTTP.send'和'objHTTP.setRequestHeader'行存在。我已经在单独的requestheader行上尝试了它们,没有'authorisation'标签和许多其他随机的东西。有谁能指出我在这里失败的地方?

非常感谢您的光临!

0 个答案:

没有答案