I am attempting some first time script writing to build a script that will auto-forward emails received in a one inbox (let’s say mine) to another email based on the incoming sender’s membership to a label in my Contact list. Seemed simple enough… but I am a first time coder, using some resources and ChatGPT.
I’ve done the following:
- Enabled People API and connected the right project along with OAuths.
- Json scripted the following:
- –“https://www.googleapis.com/auth/contacts.readonly”
- –“https://www.googleapis.com/auth/gmail.modify”
- I have my label resource ID.
My problem is that that code executes but does not pull from the people API or simply won’t forward the emails.
I’m getting this error: “Error during script execution: GoogleJsonResponseException: API call to people.people.connections.list failed with error: Invalid personFields mask path: “group_membership”. Valid paths are documented at https://developers.google.com/people/api/rest/v1/people/get.”
Thanks!!
I’m using various versions of this code (I cut out the logging code). Feel free to point out the stupid mistakes… very much still learning this (like 2 days in).
`function forwardAndArchiveEmails() {
var forwardingAddress = "[email protected]";
var groupId = "6d642cc6887790e9";
// Fetch all contacts with their email addresses and memberships
var response = People.People.Connections.list('people/me', {
personFields: 'emailAddresses,memberships',
pageSize: 1000
});
var emailAddresses = [];
if (response && response.connections) {
response.connections.forEach(function(person) {
if (person.memberships) {
person.memberships.forEach(function(membership) {
if (membership.contactGroupMembership &&
membership.contactGroupMembership.contactGroupId === groupId) {
if (person.emailAddresses) {
person.emailAddresses.forEach(function(emailInfo) {
emailAddresses.push(emailInfo.value);
});
}
}
});
}
});
}
var threads = GmailApp.search('is:unread', 0, 5);
threads.forEach(function(thread) {
var messages = thread.getMessages();
messages.forEach(function(message) {
var fromAddress = parseSenderAddress(message.getFrom());
if (emailAddresses.includes(fromAddress)) {
message.forward(forwardingAddress);
thread.moveToArchive();
}
});
});
}
function parseSenderAddress(fromField) {
var match = fromField.match(/<(.+)>/);
return match ? match[1] : fromField;
}`
I’m getting this error: “Error during script execution: GoogleJsonResponseException: API call to people.people.connections.list failed with error: Invalid personFields mask path: “group_membership”. Valid paths are documented at https://developers.google.com/people/api/rest/v1/people/get.”
Thanks!!
Logan Brown is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.