9. Juli 2024 14:25
9. Juli 2024 18:27
10. Juli 2024 11:46
10. Juli 2024 15:41
url := 'https://api-eu.dhl.com/parcel/de/account/auth/ropc/v1/token';
TextBody := 'grant_type=password&username=admin&password=xxxxx&client_id=xxxxxxxxxxxxx&client_secret=xxxxxxxxxx';
HttpWebRequestMgt.Initialize(URL);
HttpWebRequestMgt.SetMethod('POST');
HttpWebRequestMgt.SetReturnType('application/json');
HttpWebRequestMgt.SetContentType('application/x-www-form-urlencoded');
HttpWebRequestMgt.AddBodyAsAsciiText(TextBody);
TempBlob.Blob.CREATEINSTREAM(inStream);
IF NOT HttpWebRequestMgt.GetResponse(inStream,HttpStatusCode,ResponseHeaders) THEN
HttpWebRequestMgt.ProcessFaultResponse(Response);
11. Juli 2024 11:07
LOCAL [TryFunction] GetAuthToken2()
URL := 'https://api-eu.dhl.com/parcel/de/account/auth/ropc/v1/token';
HttpClient := HttpClient.HttpClient();
MediaTypeWithQualityHeaderValue := MediaTypeWithQualityHeaderValue.MediaTypeWithQualityHeaderValue('application/x-www-form-urlencoded');
HttpClient.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue);
Arr := Arr.CreateInstance(GETDOTNETTYPE(Type),2);
Arr.SetValue(GETDOTNETTYPE(String),0);
Arr.SetValue(GETDOTNETTYPE(String),1);
Type := GETDOTNETTYPE(Dictionary);
Type := Type.MakeGenericType(Arr);
Dictionary := Activator.CreateInstance(Type);
Dictionary.Add('grant_type','password');
Dictionary.Add('client_id','XXXXXXX');
Dictionary.Add('username','XXXXXXX');
Dictionary.Add('password','XXXXXXX');
Dictionary.Add('client_secret','XXXXXXX');
FormUrlEncodedContent := FormUrlEncodedContent.FormUrlEncodedContent(Dictionary);
HttpResponseMessage := HttpClient.PostAsync('https://api-eu.dhl.com/parcel/de/account/auth/ropc/v1/token',FormUrlEncodedContent).Result;
HttpResponseMessage.EnsureSuccessStatusCode();
Text_Body := FORMAT(HttpResponseMessage.Content.ReadAsStringAsync().Result);
JSONTextReader := JSONTextReader.JsonTextReader(StringReader.StringReader(Text_Body));
IF JSONTextReader.Read THEN BEGIN
REPEAT
IF TextBody <> '' THEN
TextBody += CRFL;
TextBody += STRSUBSTNO('%1 = %2 / %3',JSONTextReader.Path,JSONTextReader.Value,JSONTextReader.TokenType);
UNTIL NOT JSONTextReader.Read;
END;
MESSAGE(TextBody);
string url = "https://api-eu.dhl.com/parcel/de/account/auth/ropc/v1/token";
var httpClient = new HttpClient();
// Formulate the content data
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "XXXXXX"),
new KeyValuePair<string, string>("client_id", "XXXXXX"),
new KeyValuePair<string, string>("username", "XXXXXX"),
new KeyValuePair<string, string>("password", "XXXXXX"),
new KeyValuePair<string, string>("client_secret", "XXXXXX")
});
// Send the POST request
var response = await httpClient.PostAsync(url, content);
// Check if the request was successful
if (response.IsSuccessStatusCode)
{
// Read and display the response from the API
var responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseContent);
}
else
{
Console.WriteLine("Failed to retrieve access token.");
}