Hey everyone, I’m pulling my hair out over here! I’ve been trying to set up a contact sync between two Google Workspace accounts using Google Apps Script, but I keep running into a roadblock.
I wrote a script that’s supposed to grab contacts from both accounts and sync any new ones. But when I run it, I get this annoying error:
Error fetching contacts: {
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential.",
"status": "UNAUTHENTICATED"
}
}
I’ve already tried a bunch of things to fix it:
- Made sure the People API is turned on for both accounts
- Set up and shared credentials between the accounts
- Added some redirect URIs (localhost, script.google.com/oauthcallback, and the OAuth playground)
- Got fresh refresh tokens from the OAuth playground for both accounts
But nothing seems to work! The script just won’t authenticate properly. Any ideas on what I might be missing or doing wrong? I’m totally stuck and could really use some expert advice. Thanks in advance for any help!
I’ve dealt with similar authentication issues before, and it can be quite frustrating. One thing that worked for me was double-checking the scopes in my OAuth consent screen. Make sure you’ve included https://www.googleapis.com/auth/contacts and https://www.googleapis.com/auth/contacts.readonly.
Also, are you using the correct client ID and secret in your script? Sometimes I’ve accidentally used the wrong credentials, which caused similar errors. It might be worth regenerating your client secret just to be safe.
Lastly, have you considered using the Advanced Google Services instead of directly calling the People API? It can simplify the authentication process significantly. You’d need to enable it in the Apps Script editor under Services, but it might save you some headaches in the long run.
heya dancingbutterfly, sounds like a real headache! have u tried using service accounts instead of oauth? they’re easier to set up for cross-account stuff. just create one in each workspace, give it domain-wide access, and use the private key in ur script. might save u some trouble with auth. good luck!
I’ve encountered similar issues when working with Google Workspace accounts. One often overlooked step is ensuring that the OAuth consent screen is properly configured for both accounts. Verify that you’ve set the correct app name, user support email, and developer contact information. Additionally, check if you’re using the correct API credentials for each account.
Another potential solution is to implement exponential backoff in your script. Sometimes, these errors occur due to rate limiting or temporary API issues. By adding a retry mechanism with increasing delays between attempts, you might overcome transient authentication problems.
If all else fails, consider using a library like googleapis to handle authentication. It can simplify the process and handle token refresh automatically, potentially resolving your authentication woes.