通过Excel跟踪UPS

时间:2019-12-22 17:43:40

标签: json excel vba ups

我正在尝试构建一个Excel工作表,该工作表将提取UPS跟踪信息。以下代码在If语句中失败,并且似乎与UPS未返回正确的信息有关。

我插入Debug.Print来查看返回了什么信息,服务器响应为:

{“ statusCode”:“ 299”,“ statusText”:“ FAIL”,“ isLoggedInUser”:false,“ trackedDateTime”:null,“ isBcdnMultiView”:false,“ trackDetails”:null}

如何通过POST语句提供登录信息?

Public Sub test()

    Debug.Print GetUPSDeliveryDate("1Z29994X0323512444")

End Sub
Public Function GetUPSDeliveryDate(ByVal id As String) As String

    Dim body As String, json As Object
    body = "{""Locale"":""en_US"",""TrackingNumber"":[""" & id & """]}"
    With CreateObject("MSXML2.XMLHTTP")
        .Open "POST", "https://www.ups.com/track/api/Track/GetStatus?loc=en_US", False
        .setRequestHeader "Referer", "https://www.ups.com/track?loc=en_US&requester=ST/"
        .setRequestHeader "User-Agent", "Mozilla/5.0"
        .setRequestHeader "DNT", "1"
        .setRequestHeader "Content-Type", "application/json"
        .setRequestHeader "Accept", "application/json, text/plain, */*"
        .send
        Set json = JsonConverter.ParseJson(.responseText)
        Debug.Print .responseText
    End With
    If json("trackDetails")(1)("packageStatus") = "Delivered" Then
        GetUPSDeliveryDate = json("trackDetails")(1)("deliveredDate")
    Else
        GetUPSDeliveryDate = "Not yet delivered"
    End If
End Function

0 个答案:

没有答案