Trouble with Google Apps Script for Contact Sync between Google Workspace Accounts

Hey everyone, I’m hitting a wall with my Google Apps Script. I’m trying to sync contacts between two Google Workspace accounts, but I keep running into an authentication issue. Here’s what I’ve done so far:

  1. Made sure the People API is turned on for both accounts
  2. Set up and shared credentials
  3. Added the right redirect URIs
  4. Got refresh tokens from Google OAuth Playground

But I’m still getting a 401 error saying my authentication is invalid. The script is supposed to grab contacts from both accounts and add any new ones to the other account. It’s using refresh tokens to get access tokens, then fetching and comparing contacts.

I’ve double-checked everything, but I’m stumped. Any ideas on what I might be missing or doing wrong? I’d really appreciate some help figuring this out!

Here’s a simplified version of what I’m trying to do:

function syncContacts() {
  const token1 = getNewAccessToken(REFRESH_TOKEN_1);
  const token2 = getNewAccessToken(REFRESH_TOKEN_2);

  const contacts1 = fetchContactList(token1);
  const contacts2 = fetchContactList(token2);

  addNewContacts(contacts1, token2);
  addNewContacts(contacts2, token1);
}

function fetchContactList(token) {
  // API call to get contacts
}

function addNewContacts(sourceContacts, targetToken) {
  // Compare and add new contacts
}

function getNewAccessToken(refreshToken) {
  // Get new access token using refresh token
}

Any thoughts on why I’m getting the authentication error?

have u tried using service accounts instead of oauth? they’re easier to set up and manage for this kinda thing. might solve ur auth issues. also, double check ur scopes - sometimes that can cause 401 errors if they’re not set right. good luck with ur script!

I’ve encountered similar issues before. One thing that’s often overlooked is token expiration. Even with refresh tokens, there can be issues if the initial authorization wasn’t set up correctly. Have you verified that your refresh tokens are actually working and generating new access tokens? Also, check if you’re hitting any API quotas or limits. Google can be quite strict with those, especially when syncing large numbers of contacts. Another potential issue could be with the scopes - ensure you’ve requested all necessary scopes for both reading and writing contacts. Lastly, double-check that the service account or project you’re using has the correct permissions in both Workspace accounts. Sometimes it’s a permissions issue rather than an authentication problem.

I’ve dealt with similar contact sync issues before, and one thing that often gets overlooked is proper error handling. When you’re working with multiple API calls and authentication steps, it’s crucial to implement robust error catching and logging.

Try wrapping your main functions in try-catch blocks and log detailed error messages. This can help pinpoint exactly where the 401 error is occurring. Also, consider implementing exponential backoff for API requests to handle potential rate limiting.

Another aspect to check is the token storage and retrieval process. Make sure you’re securely storing the refresh tokens and that they haven’t expired or been revoked. Sometimes, issues can arise if tokens are accidentally shared or exposed.

Lastly, verify that both Google Workspace accounts have the necessary admin permissions to allow this kind of sync operation. Sometimes, organization-level policies can interfere with these processes, even if the API is enabled and credentials are set up correctly.