Forum Discussion
_anomDiebolt_
8 years agoQrew Elite
Apparently DHL has an API that supports JavaScript and authenticates using an OAuth 2.0 Bearer Token. I was able to us their sample code and test credentials to receive an access token. I didn't go further as I don't have a DHL account but this appears to be straightforward.
See: https://pwa.tips/2017/08/24/Issue-7.html
const serverUrl = "https://api-qa.dhlecommerce.com/v1";
function getAuthToken(clientId, clientSecret, callback) {
var endpoint = "/auth/accesstoken";
// Creating new instance of XMLHttpRequest
var xhr = new XMLHttpRequest();
// Configure request
xhr.open('GET', serverUrl + endpoint, true); // true for asynchronous request
// Encoding credentials in base-64 form
var authString = "Basic " + btoa(clientId + ":" + clientSecret);
// Setting HTTP headers
xhr.setRequestHeader('Accept', 'application/json');
xhr.setRequestHeader('Authorization', authString);
// Send request to sever
xhr.send();
xhr.onreadystatechange = function() {
// If request is not complete - return
if (this.readyState != 4) return;
// If response status code is not OK
if (this.status != 200) {
console.log("Error: " + (this.status ? this.statusText : "reqeust error"));
return;
}
// Call our callback function to pass the result of the request
callback(this.responseText);
};
}
var clientId = "e9ed82a8-4237-4185-8e36-47264aa9e718";
var clientSecret= "b1ed1bfa-689b-4d5b-bbf2-39dde64ccb64";
callback = function(result) {
console.log(JSON.stringify(result, null, " "));
}
//output:
{
"access_token": "Cf5QrrLDJ6cnHEcD9m06j3HNOf22Lbjvjr58kKKb6Ow31di9Z0M8nE",
"token_type": "Bearer",
"expires_in": 18000,
"scope": "shipping efulfillment label"
}
See: https://pwa.tips/2017/08/24/Issue-7.html