Go Down

Topic: Access to XMLHttpRequest CORS ERROR (Read 6398 times) previous topic - next topic

apetruzzella71

Hi, I'm trying to create an Ionic App using IOT - API to get values from Arduino Cloud. This is my code:


Code: [Select]
  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.

pert

I'm not knowledgeable on this topic, but it looks like some related information here:
https://github.com/arduino/arduino-create-agent-js-client/issues/253
Maybe that will be helpful to you.

apetruzzella71

#2
Dec 14, 2019, 03:15 pm Last Edit: Dec 16, 2019, 04:32 pm by apetruzzella71
thanks Pert for your interest but Arduino agent concerns development environment on windows or other platforms but not development of apps on android or ios

Go Up