0

This is the Error:

Exception: Unexpected error while getting the method or property getContacts on object ContactsApp. (line 2, file "ag1")

The Original Question:

The code came from here. It's a previous answer to a question. I don't understand how I can be getting that error there.

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')
}

Actually, I can run the code in another spreadsheet with no errors but in the spreadsheet that I use all of the time with StackOverflow Questions it fails.

This function fails on line 2 also:

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

After Fix For UrlFetch Issue Roll Out:

Still getting the same error in this file. It still works in other files. Here's my manifest file (minus my libraries):

{
  "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"
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • I don't understand. The Error is occurfing on line 2. – Cooper Dec 15 '20 at 20:28
  • Pretty sure it's a duplicate of https://stackoverflow.com/questions/65233801/unexpected-error-on-urlfetchapp-fetch-in-google-apps-script-using-basic-authenti Under the hood, `getContacts` seems to be using `UrlFetchApp` – TheMaster Dec 15 '20 at 21:34
  • You're probably right. Thanks. Should I just delete the question? – Cooper Dec 15 '20 at 21:48
  • No. But you might wanna add a comment the issuetracker stating ContactsApp also does the same. Maybe even link this post there. – TheMaster Dec 15 '20 at 22:27
  • Okay. Thanks for the info – Cooper Dec 15 '20 at 22:30
  • 1
    Are you still able to reproduce this? I cannot reproduce this, and it looks like a fix for the UrlFetch issue has rolled out, based on [this comment](https://issuetracker.google.com/issues/175141974#comment377). – Iamblichus Dec 16 '20 at 09:10
  • 1
    @lamblichus Still receiving the same error. I added most of my manifest file. I don't see any missing scopes. – Cooper Dec 16 '20 at 18:41
  • @Cooper Did you comment in issuetracker mentioning this issue? – TheMaster Dec 16 '20 at 18:57
  • 1
    @TheMaster I just completed the comment. – Cooper Dec 16 '20 at 19:12

2 Answers2

3

You need to enable Contacts API in the GCP project.

Based on your comment in Issue Tracker:

It's a cloud platform project.

I think the issue here is that you haven't enabled Contacts API in the GCP project. Once you do that, this problem should go away.

Therefore, I don't think this problem is related to the UrlFetch issue.

Update - Regarding standard and advanced services:

@TheMaster It's not just advanced services that require enabling the corresponding API in the GCP project; some of the standard services, including the current one or, for example, DriveApp.getRootFolder(), require that. See this issue:

Specifically 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.

At this point it's still unclear whether this is a problem of the documentation, which should mention not only Advanced Services, or if it's a bug related to these failing standard services.

In any case, this has been reported internally in Issue Tracker and it's still under investigation. And the issue the OP is having can be fixed by enabling Contacts API in the GCP project.

Reference:

Iamblichus
  • 18,540
  • 2
  • 11
  • 27
  • Enabling a API is only needed if you use Advanced Google services or the contacts API directly and not required if you use the inbuilt wrapper: `ContactsApp` – TheMaster Dec 17 '20 at 11:51
  • 1
    @TheMaster Currently, not only Advanced Services require enabling the API in the GCP project. See the Update section of my answer. – Iamblichus Dec 17 '20 at 12:22
  • I believe `va....@google.com` is clearly mistaken and the documentation is clear. You don't need to enable any APIs for standard services. In any case, If you're right, @Cooper's issue 1.should be fixed by enabling the api and 2. should re occur consistently on disabling the api. I'll wait for OP to confirm. – TheMaster Dec 17 '20 at 13:26
  • @TheMaster Have you tested any of the mentioned methods? I have been experiencing this behaviour consistently, for all these methods. It could be that all of these errors are caused by different issues, but the fact all methods start working after enabling the corresponding API (and stop working when it is disabled) is suspicious at least! In any case, as I said, this was reported internally and the jury's still out on what's causing this. – Iamblichus Dec 17 '20 at 13:56
  • 1
    I can confirm that your workaround using enabling the api works in a non-apps script managed platform, but none of this is documented and it is not supposed to work this way either. This should be a bug. – TheMaster Dec 17 '20 at 14:39
  • @TheMaster A documentation bug was filed internally for this, and updates about this will be communicated in the public issue I referenced. Depending on the outcome of this, a regular bug (or several) will have to be filed. – Iamblichus Dec 17 '20 at 14:58
  • I really don't think we can establish this as a norm based on 1 comment on a thread with two stars. I really think `va...@google.com` is mistaken on this issue and that it really is a bug. – TheMaster Dec 17 '20 at 15:04
  • @TheMaster Whether this is a bug (or several) or not, in theory, will be determined by the internal bug I mentioned. But you're free to report this as a new issue; _maybe_ it will not get closed as duplicate and it could help prioritizing and clarifying this. – Iamblichus Dec 17 '20 at 15:31
  • Thankyou I didn't realize that I had to do that. Infact, I don't have to do it with apps script projects (how would I?). – Cooper Dec 17 '20 at 17:43
  • 1
    There's still a lot of mystery associated with Cloud Platform Projects. I remember the first time I tried learning apps script. It was about 14 years ago and I finally gave up and figured that I just wasn't smart enough to figure it out. I kind of feel that way about Cloud Platform Projects. Thanks again – Cooper Dec 17 '20 at 17:52
  • It's kind of interesting to realize that just because code completion is working doesn't mean everything is setup properly. – Cooper Dec 17 '20 at 18:00
1

According to TheMaster this is probably related to this issue Evidently, getContacts() utilizes UrlFetchApp under the hood. So if you're having this sort of problem please go to the issue and star it to let them know that you're having the problem too. The more people that have the problem I guess the more likely it will get fixed quickly.

Having said that I'll leave the question unchecked on the outside chance that someone else can provide an even better answer.

lamblichuus reports that the UrlFetch issue rollout has occurred but I'm still seeing the same error so I'm guessing this may not be the answer.

Cooper
  • 59,616
  • 6
  • 23
  • 54