B4J Question [solved] DHL-Internet Poststamps

DonManfred

Expert
Licensed User
Longtime User

Sandman

Expert
Licensed User
Longtime User
The Userresource should give an Accesstoken back.
The documentation mention that you need this:



grant_type REQUIRED. Must be set to "client_credentials".

client_id REQUIRED (aka client_id (api key) -- obtained in DHL Developer Portal)

client_secret REQUIRED (aka client_secret -- obtained in DHL Developer Portal)

username REQUIRED.

password REQUIRED.


Do you have all of that?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Do you have all of that?
Yes. I also have a wallet registered and with balance.

i tried with
B4X:
    Dim su As StringUtils

    Dim values As Map
    values.Initialize
    values.Put("grant_type",su.EncodeUrl("client_credentials","UTF-8"))
    values.Put("username",su.EncodeUrl(apiusername,"UTF-8"))
    values.Put("password",su.EncodeUrl(apipassword,"UTF-8"))
    values.Put("client_id",su.EncodeUrl(apikey,"UTF-8"))
    values.Put("client_secret",su.EncodeUrl(apisecret,"UTF-8"))

    Dim JSON As JSONGenerator
    JSON.Initialize(values)
    Dim data As String =   JSON.ToPrettyString(1)


    Dim j As HttpJob
    j.Initialize("",Me)
    'j.PostMultipart("https://api-eu.dhl.com/post/de/shipping/im/v1/user",values,Null)
    j.PostBytes("https://api-eu.dhl.com/post/de/shipping/im/v1/user",data.GetBytes("UTF8"))
    j.GetRequest.SetHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
    'headers.Put("Content-Type", "application/x-www-form-urlencoded")
    J.GetRequest.SetContentEncoding("UTF-8")
    'J.GetRequest.SetContentType("application/x-www-form-urlencoded")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    End If
    j.Release

but it only results in an error.

ResponseError. Reason: Unsupported Media Type, Response: {"fault":{"faultstring":"Unsupported Encoding \"UTF-8\"","detail":{"errorcode":"protocol.http.UnsupportedEncoding"}}}
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
It's difficult for me to help you in any real way without having your credentials and all that, and I can't imagine you want to share them so I'll just give you my thoughts and guesses. Perhaps something can help.

When you put the values in the values map, I'm not certain it's correct to encode the values. It might not make any difference, but I would assume those values would be standard ascii that wouldn't need encoding. I don't think this is your main issue, though - it's just a note if all else fails.

The server clearly dislikes you using UTF-8. As far as I can tell, the only place it should be able to see the name of the encoding is on line 20. I'd test removing the semicolon and the charset specification. If that doesn't improve anything, also remove line 22.

I am not entirely certain that line 20 is correct. It might need to be like your commented out code:
B4X:
j.GetRequest.SetContentType("application/x-www-form-urlencoded")
(But in reality they might do the same - I don't know.)

I have a vague memory that the order of setting things in HttpJob matters. I don't remember the details though...

Sorry I can't give definitive help, I hope this at least gave some ideas.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Try this:
B4X:
Sub AppStart (Args() As String)
    GetAuthorizationToken
    StartMessageLoop
End Sub

Sub GetAuthorizationToken
    Log("Requesting authorization token...")
    Dim job As HttpJob
    job.Initialize("", Me)
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append("grant_type=client_credentials").Append("&")
    sb.Append("username=franz.klammer").Append("&")
    sb.Append("password=abfahrt123#").Append("&")
    sb.Append("client_id=XjSnyVWgQp1ShIQ5HQ6Vq5PIYLN2jGNS").Append("&")
    sb.Append("client_secret=TICgJWGRysH7mA57")
    Dim body As String = sb.ToString
    job.PostString("https://api-eu.dhl.com/post/de/shipping/im/v1/user", body)
    job.GetRequest.SetHeader("content-type", "application/x-www-form-urlencoded")
    job.GetRequest.SetHeader("Content-Length", 0)
    Wait For (job) JobDone (job As HttpJob)
    If job.Success Then
        Dim response As Map = job.GetString.As(JSON).ToMap
        Log(response)
    Else
        'Log(job.ErrorMessage)
        Dim response As Map = job.ErrorMessage.As(JSON).ToMap
        Log(response)
    End If
    job.Release
End Sub
 

Attachments

  • DHL.zip
    1.1 KB · Views: 22
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Thank you.
It may work in future.

It seems i need to get my "App" approved first or something.
firefox_l0f2iEpdSQ.png

Apps with App Stats in progress do not have access it seems.
A friend told me i should get post from them first.
Need to wait a few days i guess.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
At the end i solved it with help from a german Member


Thank you @JanG for the help!

The solution was to create a new app, adding the same service, wait for approvement. This time my app was directly in Approved state

firefox_nIFQZ1Gzye.png


This thread can be closed. I´ll post some findings soon.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
This mean your code in post #3 works if you have solved the registration issue.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
This mean your code in post #3 works if you have solved the registration issue.
Basically yes. Honestly i just used the code from the german member against my credentials. after i saw his comment.

And, yes. After i solved the App-Issue the code appears to work.

Based on these findings i was able to do more things with the Api so far. I just posted a Tutorial about it for anyone Interested.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I see. I am clear now.
I was travelling with my time machine on 22 Feb 2025 to 1 Mar 2025 to copy the code from him and posted above.
Unfortunately you didn't test my code.
 
Upvote 0
Top