Hi, I'm trying to create an Ionic App using IOT - API to get values from Arduino Cloud. This is my code:
async tryOn(){
var ArduinoIotClient = require('@arduino/arduino-iot-client');
var client = ArduinoIotClient.ApiClient.instance;
client.defaultHeaders={'Access-Control-Allow-Origin':'*','Access-Control-Allow-Methods':'POST, GET, OPTIONS, PUT'};
var oauth2 = client.authentications['oauth2'];
oauth2.accessToken = await this.getToken();
var api = new ArduinoIotClient.DevicesV2Api(client);
var opts = {
'acrossUserIds': true
};
api.devicesV2List(opts).then(devices => {
console.log(devices);
}, error => {
console.log(error)
});
}
async getToken(){
return new Promise(access_token=>{ this.http.sendRequest ('https://login.arduino.cc/oauth/token',{
method:'post',
data:{
grant_type: 'client_credentials',
client_id: ***,
client_secret:***,
audience: 'https://api2.arduino.cc/iot'
}
}).then((data)=>{
access_token(JSON.parse(data["data"])["access_token"]);
},error=>{
access_token(error);
});
});
}
getToken method, where I have to use native ionic http client class, works perfectly, and client object in TryOn Method has its acces token received by server and passed by function.
But api.devicesV2List ends with this error:
Access to XMLHttpRequest at 'http://api2.arduino.cc/iot/v2/things' from origin 'http://localhost' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
As you can see, I try to add headers params to client to avoid CORS problem, but without success.
Any help appreciated, thank you
A.