2

I am working on one project for a client, and I need to add contacts to Google Contacts with Google Contacts API. The code:

function createContact(data) {
  var contact = ContactsApp.createContact(data.firstName, data.lastName, data.email);
  contact.addAddress(data.address);
}

runs on the trigger and I am getting this error: Exception: You do not have permission to perform that action. (the same happens when I run the function from the Editor). I added permissions to the project (Permissions screenshot) and scope to the appsscript.json file:

"oauthScopes": [
    "https://www.google.com/m8/feeds"
]

Additional question: If I have got this project in my Google Account, how I can access to Google Contacts and add contacts to the client's Google account?

Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54
Matu
  • 33
  • 1
  • 9
  • You should consider using the people api – Linda Lawton - DaImTo Aug 12 '21 at 12:26
  • If you can get your client to setup a shared drive for you then your access would be greatly improved. – Cooper Aug 12 '21 at 13:49
  • Is it the `ContactsApp.createContact(data.firstName, data.lastName, data.email)` that throws this error? Or another line? Have you reauthorised the script after adding the scope? – Rafa Guillermo Aug 13 '21 at 08:01
  • It's very likely the trigger that is not authorized to do that. What kind of trigger are you running? – Dmitry Kostyuk Aug 13 '21 at 11:20
  • Thank you all for your comments. Yes, I am using a simple trigger, so maybe that is the problem, but sending emails works with the simple trigger although it requires authorization (and I am getting the error also when I run it from the Editor). I will use People API instead of Contacts API. – Matu Aug 14 '21 at 08:09

1 Answers1

1

I'm assuming that you are running a simple trigger. This is what the official documentation says about this:

They cannot access services that require authorization. For example, a simple trigger cannot send an email because the Gmail service requires authorization, but a simple trigger can translate a phrase with the Language service, which is anonymous.

You probably need to change your workflow and use an installable trigger or something else. If you need help with that, feel free to create a new question.

Dmitry Kostyuk
  • 1,354
  • 1
  • 5
  • 21