0

我使用 Google 应用程序脚本通过设备访问控制台将我的 Nest Thermostat 记录到 Google 工作表中。

我能够手动记录数据,并且初始定时触发器执行并在电子表格上记录数据。但在那之后,我必须重新授权并手动运行 logthermostatdata 功能。

看来我的回调有问题,最终会过期。当我运行 authCallback(request) 时,我收到以下错误:

TypeError: Cannot read property 'parameter' of undefined Service_.handleCallback @  Service.gs:390

任何人都知道如何解决这个问题或有同样的问题?

代码和错误截图

/**
 * Logs the redirect URI set up for the client
 *  -- run this function to manually grant permissions
 */
function logRedirectUri() {
  var service = getService();
  Logger.log(service.getRedirectUri());
  service.reset();
  if (service.hasAccess()) {
      // get the access token
      const access_token = service.getAccessToken();
  } else {
    var authorizationUrl = service.getAuthorizationUrl();
    Logger.log('Open the following URL and re-run the script: %s', authorizationUrl);
  }
}

/**
 * Create the OAuth2 service helper
 */
function getService() {
  // Create a new service with the given name. The name will be used when
  // persisting the authorized token, so ensure it is unique within the
  // scope of the property store.
  return OAuth2.createService("nestservice")
      // Set the endpoint URLs.
      .setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/v2/auth')
      .setTokenUrl('https://oauth2.googleapis.com/token')

      // Set the client ID and secret.
      .setClientId(OAUTH_CLIENT_ID)
      .setClientSecret(OAUTH_CLIENT_SECRET)

      // Set the name of the callback function that should be invoked to
      // complete the OAuth flow.
      .setCallbackFunction('authCallback')

      // Set the property store where authorized tokens should be persisted.
      .setPropertyStore(PropertiesService.getUserProperties())

      // Set the scope and additional Google-specific parameters.
      .setScope('https://www.googleapis.com/auth/sdm.service')
      .setParam('access_type', 'offline')
      .setParam('approval_prompt', 'force')
      .setParam('login_hint', Session.getActiveUser().getEmail());
}

/**
 * Report success or failure of permissions
 */
function authCallback(request) {
  const service = getService();
  const isAuthorized = service.handleCallback(request);
  if (isAuthorized) {
    return HtmlService.createHtmlOutput('Success! You can close this tab.');
  } else {
    return HtmlService.createHtmlOutput('Denied. You can close this tab');
  }
}

4

0 回答 0