I am working on this for my private home network so security is not really an issue. I'm trying to get sign authorized through a browser, using an HTML file with vanilla JavaScript. I thought this was all I need but I keep getting "sign invalid" and I can't figure out why....
I keep getting "sign invalid" and I can't figure out why. I would gladly buy someone a beer or coffee if you can tell me where I'm going wrong?
I should mention that I am able to get authorized in Postman, the Tuya API explorer and Node.js. The code below is the modified Node.js code...
Code: Select all
var ClientID = 'xxxxxxxxxxxxxxxxx';
var secretKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';
getToken();
async function getToken(){
var method = 'GET';
var timestamp = Date.now().toString();
var signUrl = '/v1.0/token?grant_type=1';
var contentHash = CryptoJS.HmacSHA256(signStr, secretKey);
var stringToSign = [method, contentHash, '', signUrl].join('\n');
var signStr = ClientID + timestamp + stringToSign;
console.log(await encryptStr(signStr, secretKey));
fetch('https://openapi.tuyaus.com//v1.0/token?grant_type=1', {
method: 'GET',
headers: {
't': timestamp,
'sign_method': 'HMAC-SHA256',
'client_id': ClientID,
'sign': await encryptStr(signStr, secretKey)
},
}).then(function(response) {var json = response.json();console.log(json);})
}
async function encryptStr(signStr, secretKey) {
var hash = CryptoJS.HmacSHA256(signStr, secretKey);
var hashInBase64 = hash.toString().toUpperCase();
return hashInBase64;
}