Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3655

VBA Rest API - DHL Return label - 401

$
0
0

I am trying to create a return shipping DHL parcel label with provided DHL api in the sandbox:https://developer.dhl.com/api-reference/parcel-de-returns-post-parcel-germany#get-started-section/user-guide

With postman it works. But I would like to implement the HTTP Request in VBA.I always receive status 401 Unauthorized

I guess it is the way how I pass the credentials.Anyone has an idea how to get it working?

create return labels in the sandbox, with the following user data:Username: "2222222222_customer"Password: "uBQbZ62!ZiBiVVbhc"

Sub restAPICall()Dim objRequest As MSXML2.ServerXMLHTTP60Dim id_header_name As String, id_key As String, secret_header_name As String, secret_key As StringDim strUrl As StringDim blnAsync As BooleanDim strResponse As StringDim json As ObjectDim authKey As StringSet objRequest = New ServerXMLHTTP60strUrl = "https://api-sandbox.dhl.com/parcel/de/shipping/returns/v1/orders?labelType=BOTH" 'Endpoint TestblnAsync = Falseid_key = "2222222222_customer"pass = "uBQbZ62!ZiBiVVbhc"apiKey = "123456789"body = "{""receiverId"":""deu"", " _& " ""customerReference"":""Kundenreferenz"", " _& " ""shipmentReference"":""Sendungsreferenz"", " _& " ""shipper"": { " _& " ""name1"":""Absender Retoure Zeile 1"", " _& " ""name2"":""Absender Retoure Zeile 2"", " _& " ""name3"":""Absender Retoure Zeile 3"", " _& " ""addressStreet"":""Charles-de-Gaulle Str."", " _& " ""addressHouse"":""20"", " _& " ""city"":""Bonn"", " _& " ""email"":""Max.Mustermann@dhl.local"", " _& " ""phone"":""+49 421 987654321"", " _& " ""postalCode"":""53113"", " _& " ""state"":""NRW"", " _& " }, " _& " ""itemWeight"": { " _& " ""uom"": ""g"", " _& " ""value"":""1000"", " _& " }, " _& " ""itemValue"": { " _& " ""currency"": ""EUR"", " _& " ""value"":""100"", " _& " }, " _& "}"With objRequest    .Open "POST", strUrl, blnAsync ', gkpuser, gkpass    .setRequestHeader "Authorization", "Basic " + EncodeBase64(id_key +":" + pass)    .setRequestHeader "Content-Type", "application/json"    .setRequestHeader "Accept", "application/json"    .setRequestHeader "dhl-api-key", "apiKey"    .Send body    While objRequest.readyState <> 4        DoEvents    Wend    strResponseHeaders = .StatusText    strResponse = .responseText    allResponseHeader = .GetAllResponseHeadersEnd WithDebug.Print bodyDebug.Print allResponseHeaderDebug.Print strResponseEnd Sub
Function EncodeBase64(text$)    Dim b    With CreateObject("ADODB.Stream")        .Open: .Type = 2: .Charset = "utf-8"        .WriteText text: .Position = 0: .Type = 1: b = .Read        With CreateObject("Microsoft.XMLDOM").createElement("b64")            .DataType = "bin.base64": .nodeTypedValue = b            EncodeBase64 = Replace(Mid(.text, 5), vbLf, "")        End With        .Close    End WithEnd Function

Many thanks :)

Alex


Viewing all articles
Browse latest Browse all 3655

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>