Building an AI agent or automated pipeline that generates, edits, and uploads videos to YouTube is a massive time-saver—until your pipeline silently crashes a week later with an invalid_grant error.
When you connect a personal script, n8n workflow, or custom YouTube agent to the YouTube Data API v3, Google puts your project in Testing mode by default. In Testing mode, Google kills your OAuth refresh token every 7 days.
If you want your YouTube agent to upload videos on autopilot without forcing you to manually re-authenticate every week, you have to push your app to Production. However, navigating between Google's developer tools—and dealing with hidden UI traps when you don't own a custom domain—trips up almost everyone on their first try. Here is the complete breakdown of the two platforms you need to use, and every single caveat you need to bypass to keep your YouTube agent connected permanently.
Before touching any settings, it helps to separate the two distinct Google tools involved in this setup:
://google.com):
To stop the 7-day token expiration, you must switch your app from Testing to In production inside the Google Cloud Console under Google Auth Platform > Audience.
When you open the Audience page, you will likely find the Publish app button disabled with this notice:
"To publish your app, you must complete your configuration on the Branding page."
If you jump over to the Branding page and leave the Authorized domains field blank—or leave an empty text input box open—Google blocks you with Invalid domain: must not be empty.
Even worse, Google's updated interface strictly requires an Application home page, a Privacy policy link, and at least one Authorized domain before unlocking the Publish button.
How to fix it without buying a domain:
Because this agent is strictly for your own YouTube channel, you do not need to buy a domain or build a website:
https://github.com) or a public Google Doc stating that the app is a private automation tool for your personal channel.https://github.com#privacy). github.com or google.com). Return to the Audience page and refresh the browser. The Publish app button will now be clickable. Click it and confirm.
Because up videos (youtube.upload) is classified as a sensitive scope, Google will immediately prompt you to Submit for verification with a warning about unverified apps.
Ignore and close this prompt.
(Note: If you happen to use a paid Google Workspace account rather than a standard @gmail.com account, you can avoid publishing altogether by setting your User type to Internal—Internal apps never expire refresh tokens).
Now switch over to the Google OAuth 2.0 Playground (://google.com) to generate the actual credential your YouTube agent will use.
By default, the OAuth Playground uses Google's generic public Client ID, whose tokens expire within hours.
In Step 1 on the left sidebar, select the two scopes your YouTube agent needs to upload and edit videos:
https://googleapis.com
Click Authorize APIs. When you log in, Google will show a warning screen saying "Google hasn't verified this app." Click Advanced at the bottom left, then click Go to Your Agent Name.
Once redirected back to the Playground, look carefully at Step 2:
4/0A...) is "refresh_token" (it always starts with 1//). This is the biggest trap of all: switching your app to "In production" does not retroactively save refresh tokens you generated while in Testing mode.
Refresh tokens inherit the publishing status of your Google Cloud project at the exact second they are created. If you generated a token before clicking Publish app, throw it away and generate a brand-new one in the Playground now.
Once you paste that production "refresh_token" into your YouTube agent's .env file or workflow manager, it will automatically fetch fresh 1-hour access tokens behind the scenes forever.
Google will only invalidate a production refresh token if:
With those caveats cleared, your YouTube agent is ready to run hands-free!