Tuya api authorisation - failing due to CORS

104 Views Asked by At

I am trying to access the Tuya api and get the access token. I have followed the docs for setting up in Postman:

https://developer.tuya.com/en/docs/iot/set-up-postman-environment?id=Ka7o385w1svns

and all works fine.

Moving this into a Vue3 project and I get a CORS error.

I have checked the signature (https://developer.tuya.com/en/docs/iot/singnature?id=Ka43a5mtx1gsc) data being sent with that in Postman and they are exactly the same.

  // sha256
  var signMap = this.stringToSign(query, mode, httpMethod, secret);
  var urlStr = signMap["url"];
  var signStr = signMap["signUrl"];

  this.apiUrl = this.baseUrl + urlStr;

  const nonce = "";
  this.easy_sign = this.calcSign(
    clientId,
    timestamp,
    nonce,
    signStr,
    secret
  );

  console.log("Easy Sign", this.easy_sign); // The same as Postman

  var myHeaders = new Headers();
  myHeaders.append("client_id", clientId);
  myHeaders.append("sign", this.easy_sign);
  myHeaders.append("t", timestamp);
  myHeaders.append("sign_method", "HMAC-SHA256");
  myHeaders.append("nonce", "");
  myHeaders.append("stringToSign", "");

  var requestOptions = {
    method: "GET",
    headers: this.myHeaders,
    redirect: "follow",
  };

  fetch(this.apiUrl, requestOptions)
    .then((response) => response.text())
    .then((result) => console.log(result))
    .catch((error) => console.log("error", error));
},

Do I need to add something extra into the headers for this to work?

0

There are 0 best solutions below