Can you explain why I'm getting this error: Unexpected error while getting the method or property getContacts on object ContactsApp

Can you explain why I'm getting this error: Unexpected error while getting the method or property getContacts on object ContactsApp

这是错误:

异常:在对象 ContactsApp 上获取方法或 属性getContacts 时出现意外错误。 (第 2 行,文件“ag1”)

原题:

代码来自here。这是一个问题的先前答案。我不明白我怎么会在那里得到那个错误。

function getAllContacts() {
  var contactsA=ContactsApp.getContacts();//The Error occurs on this line
  var s='';
  var br='<br />';//line delimiter change to linefeed when not using html dialog
  var dlm=' ~~~ ';//field delimiter
  for(var i=0;i<contactsA.length;i++) {
    s+=Utilities.formatString('<br />\'%s\',\'%s\',\'%s\',\'%s\',\'%s\'%s',
    (typeof(contactsA[i].getFullName())!='undefined')?contactsA[i].getFullName():'',
    (typeof(contactsA[i].getAddresses().map(function (v,i,A) { return A[i].getAddress();}))!='undefined')?contactsA[i].getAddresses().map(function (v,i,A) { return A[i].getAddress();}).join(dlm):'',
    (typeof(contactsA[i].getEmails().map(function(v,i,A) {return A[i].getAddress();}))!='undefined')?contactsA[i].getEmails().map(function(cV,i,A) {return A[i].getAddress();}).join(dlm):'',
    (typeof(contactsA[i].getPhones().map(function(v,i,A){return A[i].getPhoneNumber();}))!='undefined')?contactsA[i].getPhones().map(function(v,i,A){return A[i].getPhoneNumber();}).join(dlm):'',
    (typeof(contactsA[i].getCompanies().map(function(v,i,A){return A[i].getCompanyName();}))!='undefined')?contactsA[i].getCompanies().map(function(v,i,A){return A[i].getCompanyName();}).join(dlm):'',br);
  }
  var ui=HtmlService.createHtmlOutput(s).setWidth(800)  ;
  SpreadsheetApp.getUi().showModelessDialog(ui, 'Contacts')
}

实际上,我可以 运行 另一个电子表格中的代码没有错误,但在我一直使用 Whosebug Questions 的电子表格中却失败了。

这个函数在第 2 行也失败了:

function testing101() {
  const cA=ContactsApp.getContacts();
  Logger.log(cA.length);
}

修复 UrlFetch 问题推出后:

在此文件中仍然出现相同的错误。它仍然适用于其他文件。这是我的清单文件(减去我的库):

{
  "timeZone": "America/Denver",
  "dependencies": {
    "enabledAdvancedServices": [{
      "userSymbol": "Drive",
      "serviceId": "drive",
      "version": "v2"
    }, {
      "userSymbol": "People",
      "serviceId": "peopleapi",
      "version": "v1"
    }, {
      "userSymbol": "Slides",
      "serviceId": "slides",
      "version": "v1"
    }, {
      "userSymbol": "Gmail",
      "serviceId": "gmail",
      "version": "v1"
    }, {
      "userSymbol": "Sheets",
      "serviceId": "sheets",
      "version": "v4"
    }, {
      "userSymbol": "DriveActivity",
      "serviceId": "driveactivity",
      "version": "v2"
    }, {
      "userSymbol": "Calendar",
      "serviceId": "calendar",
      "version": "v3"
    }],
  "webapp": {
    "access": "MYSELF",
    "executeAs": "USER_DEPLOYING"
  },
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": ["https://mail.google.com/", "https://www.google.com/m8/feeds", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/script.external_request", "https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/script.container.ui", "https://www.googleapis.com/auth/calendar", "https://www.googleapis.com/auth/gmail.send", "https://www.googleapis.com/auth/script.send_mail", "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/presentations", "https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/documents", "https://www.googleapis.com/auth/script.projects.readonly", "https://sites.google.com/feeds", "https://www.googleapis.com/auth/drive.activity"],
  "runtimeVersion": "V8"
}

根据 TheMaster 的说法,这可能与 this issue 有关。显然,getContacts() 在幕后利用了 UrlFetchApp。因此,如果您遇到此类问题,请转到该问题并为其加注星标,让他们知道您也遇到了问题。遇到问题的人越多,我想它越有可能很快得到解决。

话虽如此,我会保留这个问题,以防其他人可以提供更好的答案。

lamblichuus 报告说 UrlFetch 问题推出已经发生,但我仍然看到同样的错误,所以我猜这可能不是答案。

您需要在 GCP 项目中启用联系人 API。

基于your comment in Issue Tracker

It's a cloud platform project.

我认为这里的问题是您没有在 GCP 项目中启用 Contacts API。一旦你这样做,这个问题就会消失。

因此,我认为此问题与 UrlFetch 问题无关。

更新 - 关于标准和高级服务:

@TheMaster 不仅仅是高级服务需要在GCP项目中启用相应的API;一些标准服务,包括当前的服务,或者例如 DriveApp.getRootFolder(),需要这样做。看到这个问题:

具体来说this comment:

There are mentions about enabling APIs for advanced services here. But not for the standard services, I notified this to the documentation team.

目前还不清楚这是否是文档的问题,文档不仅应该提到高级服务,还是与这些失败的标准服务相关的错误。

无论如何,这已在 Issue Tracker 内部报告,并且仍在调查中。 OP 的问题可以通过在 GCP 项目中启用联系人 API 来解决。

参考: