为什么我可以获取我的Nest恒温器ID,但其他人使用相同的脚本却不能?

huangapple go评论65阅读模式
英文:

Why I can get my Nest thermostat ID but not other people using the same script?

问题

我有一个在脚本中的listDevice函数,用于获取恒温器ID(与此处相同):

function listDevices() {
 
  // 指定端点
  const endpoint = '/enterprises/' + PROJECT_ID + '/devices';
 
  // 空数组用于保存设备数据
  let deviceArray = [];
 
  // 发送请求到智能API
  const data = makeRequest(endpoint);
  const deviceData = data.devices;
  console.log(deviceData);
 
  deviceData.forEach(device => {
    const name = device.name;
    const type = device.type;
    deviceArray.push([name,type]);
  });
 
  // 获取表格
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getActiveSheet();
 
  // 输出数据
  sheet.getRange(2,1,deviceArray.length,2).setValues(deviceArray);
}

它对我的Nest起作用,但是当尝试使其适用于具有多个Nest恒温器的其他人时,会出现此错误:

TypeError: Cannot read properties of undefined (reading 'forEach')

您知道为什么此脚本对我有效,但与其他人共享时会出现错误吗?

这是上面函数调用的makeRequest(endpoint)函数:

function makeRequest(endpoint) {
 
  // 获取智能服务
  const smartService = getSmartService();
   
  // 获取访问令牌
  const access_token = smartService.getAccessToken();
  //console.log(access_token);
 
  // 设置SMD API网址
  const url = 'https://smartdevicemanagement.googleapis.com/v1';
  //const endpoint = '/enterprises/' + PROJECT_ID + '/devices';
 
  // 设置调用的标头
  const headers = {
    'Authorization': 'Bearer ' + access_token,
    'Content-Type': 'application/json'
  }
   
  // 设置参数
  const params = {
    'headers': headers,
    'method': 'get',
    'muteHttpExceptions': true
  }
   
  // 尝试调用API
  try {
    const response = UrlFetchApp.fetch(url + endpoint, params);
    const responseBody = JSON.parse(response.getContentText());
     
    return responseBody;
  }
  catch(e) {
    console.log('Error: ' + e);
  }
}
英文:

I have that listDevice function in a script to get the thermostat ID (same as here):

function listDevices() {
 
  // specify the endpoint
  const endpoint = '/enterprises/' + PROJECT_ID + '/devices';
 
  // blank array to hold device data
  let deviceArray = [];
 
  // make request to smart api
  const data = makeRequest(endpoint);
  const deviceData = data.devices;
  console.log(deviceData);
 
  deviceData.forEach(device => {
    const name = device.name;
    const type = device.type;
    deviceArray.push([name,type]);
  });
 
  // get the Sheet
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getActiveSheet();
 
  // output the data
  sheet.getRange(2,1,deviceArray.length,2).setValues(deviceArray);
 
}

It works fine with my Nest, but when trying to make it work for another person with multiple Nest thermostats, it gives this error:

> TypeError: Cannot read properties of undefined (reading 'forEach')

Would you know why this script works for me, but when shared with someone it gives an error?

Here's the makeRequest(endpoint) function that is called by the function above:

function makeRequest(endpoint) {
 
  // get the smart service
  const smartService = getSmartService();
   
  // get the access token
  const access_token = smartService.getAccessToken();
  //console.log(access_token);
 
  // setup the SMD API url
  const url = 'https://smartdevicemanagement.googleapis.com/v1';
  //const endpoint = '/enterprises/' + PROJECT_ID + '/devices';
 
  // setup the headers for the call
  const headers = {
    'Authorization': 'Bearer ' + access_token,
    'Content-Type': 'application/json'
  }
   
  // set up params
  const params = {
    'headers': headers,
    'method': 'get',
    'muteHttpExceptions': true
  }
   
  // try calling API
  try {
    const response = UrlFetchApp.fetch(url + endpoint, params);
    const responseBody = JSON.parse(response.getContentText());
     
    return responseBody;
  }
  catch(e) {
    console.log('Error: ' + e);
  }
}

答案1

得分: 0

在将makeRequest(endpoint)函数替换为这个函数后,脚本可以正常工作,并且可以在不再运行listDevice函数的情况下提供设备的ID:

function makeRequesttest() {

  // 获取智能服务
  const smartService = getSmartService();
   
  // 获取访问令牌
  const access_token = smartService.getAccessToken();
  //console.log(access_token);
 
  // 设置SMD API的URL
  const url = 'https://smartdevicemanagement.googleapis.com/v1';
  const endpoint = '/enterprises/' + PROJECT_ID + '/devices';
 
  // 设置调用的标头
  const headers = {
    'Authorization': 'Bearer ' + access_token,
    'Content-Type': 'application/json'
  }
   
  // 设置参数
  const params = {
    'headers': headers,
    'method': 'get',
    'muteHttpExceptions': true
  }
   
  // 尝试调用API
  try {
    const response = UrlFetchApp.fetch(url + endpoint, params);
    const responseBody = JSON.parse(response.getContentText());
    Logger.log('response: ' + response);
    return responseBody;
  }
  catch(e) {
    console.log('Error: ' + e);
    //throw e;
  }
}
英文:

After replacing the makeRequest(endpoint) function with that one, the script works and provides me the devices ID without needing to run the listDevice function anymore:

function makeRequesttest() {
 
  // get the smart service
  const smartService = getSmartService();
   
  // get the access token
  const access_token = smartService.getAccessToken();
  //console.log(access_token);
 
  // setup the SMD API url
  const url = 'https://smartdevicemanagement.googleapis.com/v1';
  const endpoint = '/enterprises/' + PROJECT_ID + '/devices';
 
  // setup the headers for the call
  const headers = {
    'Authorization': 'Bearer ' + access_token,
    'Content-Type': 'application/json'
  }
   
  // set up params
  const params = {
    'headers': headers,
    'method': 'get',
    'muteHttpExceptions': true
  }
   
  // try calling API
  try {
    const response = UrlFetchApp.fetch(url + endpoint, params);
    const responseBody = JSON.parse(response.getContentText());
    Logger.log('response: ' + response);
    return responseBody;
  }
  catch(e) {
    console.log('Error: ' + e);
    //throw e;
  }
}

huangapple
  • 本文由 发表于 2023年1月9日 00:19:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定