Hi guys,
I am trying to use Homeyscript to log in to Bol so I can get notifications from Bol. I have API information and I think I’ve got the code correct to log in, but for some reason I get the message that I am not authorised. Could one of you help me out in figuring out what I am doing wrong?
//constants
const clientid="secret client id"
const secret="secret client password"
const loginurl="https://login.bol.com/token"
const orderlisturl="https://api.bol.com/retailer/orders"
/**
* Base64 encode a string without using btoa()
* Supports full UTF-8 characters (e.g., emojis, accents)
* Works in Node.js and browsers
* @param {string} str - The input string
* @returns {string} - Base64 encoded string
*/
function base64Encode(str) {
if (typeof str !== 'string') {
throw new TypeError('Input must be a string');
}
// Browser environment
if (typeof window !== 'undefined' && typeof window.TextEncoder === 'function') {
const encoder = new TextEncoder();
const bytes = encoder.encode(str);
let binary = '';
bytes.forEach(byte => binary += String.fromCharCode(byte));
return base64FromBinary(binary);
}
// Node.js environment
if (typeof Buffer !== 'undefined') {
return Buffer.from(str, 'utf-8').toString('base64');
}
throw new Error('No suitable Base64 encoding method found');
}
/**
* Convert binary string to Base64 manually
* @param {string} binary - Binary string
* @returns {string} - Base64 encoded string
*/
function base64FromBinary(binary) {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
let result = '';
let padding = '';
// Pad binary length to multiple of 3
const remainder = binary.length % 3;
if (remainder > 0) {
padding = '='.repeat(3 - remainder);
binary += '\0'.repeat(3 - remainder);
}
for (let i = 0; i < binary.length; i += 3) {
const chunk = (binary.charCodeAt(i) << 16) |
(binary.charCodeAt(i + 1) << 8) |
(binary.charCodeAt(i + 2));
result += chars[(chunk >> 18) & 63] +
chars[(chunk >> 12) & 63] +
chars[(chunk >> 6) & 63] +
chars[chunk & 63];
}
return result.substring(0, result.length - padding.length) + padding;
}
auth=clientid+":"+secret
auth=base64Encode(auth)
const res = await fetch(loginurl, {
method: "POST",
headers: {
'Content-Type':'application/x-www-form-urlencoded',
'Accept':'application/json',
"Authorization": "Basic "+auth
}
});
// Get the JSON
log(res)
return -1
When using ncat to listen what Homey is sending, it seems to be ok:
ncat -k -v --listen 4430
Ncat: Version 7.95 ( https://nmap.org/ncat )
Ncat: Listening on [::]:4430
Ncat: Listening on 0.0.0.0:4430
Ncat: Connection from homey:41884.
POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Accept: application/json
Authorization: Basic base64encodedstuff=
Content-Length: 0
User-Agent: node-fetch/1.0 (+https://github.com/bitinn/node-fetch)
Accept-Encoding: gzip,deflate
Host: myhost:4430
Connection: keep-alive
I get the following back from bol:
Response { size: 0, timeout: 0, [Symbol(Body internals)]: { body: PassThrough { _events: [Object], _readableState: [ReadableState], _writableState: [WritableState], allowHalfOpen: true, _maxListeners: undefined, _eventsCount: 2, [Symbol(shapeMode)]: true, [Symbol(kCapture)]: false, [Symbol(kCallback)]: null }, disturbed: false, error: null }, [Symbol(Response internals)]: { url: '``https://login.bol.com/token``', status: 401, statusText: 'Unauthorized', headers: Headers { [Symbol(map)]: [Object: null prototype] }, counter: 0 } }
This is the docs I am using to get this: