Google Apps Script: "Error 401: deleted_client The OAuth client was deleted" All of a sudden?
I currently use a Google App Scripts on Google Sheets for my budgeting spreadsheet. Essentially, the way I have it set up is:
- Buy item
- Input expense onto Google form (which feeds into a spreadsheet)
- Use Apps scripts to convert timestamp into yyyy-MM-dd
I've been using this process for 4 years but all of a sudden, today it stopped working by giving me this error below.
Error 401: deleted_client The OAuth client was deleted
Here's my app script:
Here's my trigger:
Solution 1:
At some point your Apps Script project's attached Google Cloud Platform (GCP) project (which manages the Oauth client) was likely deleted. You could confirm this by going to the Apps Script project editor, click the gear on the left for Project Settings
, then click the Project Number
link in the Google Cloud Platform (GCP) Project
section. If it loads the GCP console (using the correct Google account) but gives you an error instead of bringing you to the project, then it probably doesn't exist anymore.
First, check if it's been deleted within the past 30 days and could be recovered:
- In the GCP console, go to the
Manage resources
page (project picker → three dots button → select your organization →Manage resources
). - Below the list of organizations, folders, and projects, click
Resources pending deletion
. - If you see the project there, check the box then click
Restore
. In the dialog that appears, confirm that you want to restore the project. - Go back to the Apps Script project editor and run one of your functions, and it should allow you to authorize the project again instead of giving you the
401
error.
If the project does not appear in the Resources pending deletion
list because it'd been deleted over 30 days ago:
- Copy all of your Apps Script projects' files to your local machine. If there are many files, consider using the clasp CLI tool, which would allow you to clone the entire project locally with one
clasp pull
command. - [IMPORTANT] Confirm that all of the files have been stored locally (and backed up, if desired), because the Apps Script project will be deleted permanently.
- Make note of any services which are enabled in the
Services
section of the Apps Script project editor sidebar. - Delete the Apps Script project by going to the Apps Script project's
Overview
page, then click the trash can labeledDelete project forever
. - In your Google Sheets doc, go to
Extensions
→Apps Script
again and it will create a new Apps Script project (with attached GCP project). - Save the default
Code.gs
file - Re-create the projects' files. If you'd used
clasp pull
in the first step, then you can useclasp push
to push the files from your local machine back to the project. In this case, go to the Apps Script project'sProject Settings
page and copy theScript ID
. Then update thescriptId
in the local.clasp.json
file, save it, thenclasp push
. - If there were Services enabled in the previous project, enable them again in the Services section of the editor sidebar.
- Run one of your functions and it should allow you to authorize the project again instead of giving you the
401
error.