ambalal shah
ambalal shah

Reputation: 1

Unable to access People API in a spreadsheet app script

I am trying to connect my google form response sheet to create a new contact using details every time a response is submitted on the form. I've added Peopleapi from the "Services" tab in the apps script. I've also created the credentials for the people api from google console from the same account and I've also called for api access token. Still I am getting the error

ReferenceError: PeopleApi is not defined 

Refer the complete code below :

var apiKey = ScriptApp.getOAuthToken();
  PeopleApi.getService().setToken(apiKey);


function onFormSubmit() {

  
  // Get data from spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var lastRow = sheet.getLastRow();

  var name = sheet.getRange(lastRow, 2, 1, 1).getValue();
  var phone = sheet.getRange(lastRow, 3, 1, 1).getValue();
  var address = sheet.getRange(lastRow, 4, 1, 1).getValue();
  

  // Create a People API service object
  var peopleService = PeopleApi.getService();

  // Build the person resource
  var person = {
    names: [{
      givenName: name
    }],
  
    addresses: [{
      formattedValue: address,
      type: "home"
    }],
    phoneNumbers: [{
      value: phone,
      type: "mobile"
    }],
    metadata: {
      primary: true
    }
  };

 

  // Create the contact using the People API
  peopleService.people().createContact(person).execute();
}

I've added Peopleapi from the "Services" tab in the apps script. I've also created the credentials for the people api from google console from the same account and I've also called for api access token.

Upvotes: 0

Views: 323

Answers (1)

Wicket
Wicket

Reputation: 38160

The default identifier for the Peopleapi Service is People.

Add Service dialog showing the default identifier of the Peopleapi

You should double-check what is the identifier set for this Service in your Google Apps Script project. For this, look at the corresponding item below the Services section

Google Apps Script Sidebar

In the above image, the item for the Peopleapi is People. Clicking on this item will show a dialog to change the Service settings, including the identifier.

Most Google Apps Script users will keep the default identifier name, so I suggest you keep it, too.

Service dialog showing the identifier set for the Peopleapi

On the other hand, getService() is not a method of Peopleapi.

Automcomplete options for People.get

I wonder if you have written the code from scratch or used a tool to generate it. When using code generation tools, it's highly recommended to spend some time learning the basics of the IDE, programming language and services that you will use to be able to identify if the the code generation tool is giving helpful something. Please remember that tools like ChatGPT might have AI hallucinations.

Related

Upvotes: 1

Related Questions