Automating Google OAuth Setup End-to-End with Playwright Browser Control
Problem / Context
Setting up YouTube Data API v3 access required clicking through Google Cloud Console manually: create project, enable API, configure OAuth consent screen, create credentials, complete auth flow. For an agent that needs to provision API access across multiple Google services, this manual process took 20-30 minutes per service and could not be scheduled or repeated without human intervention.
Solution
Split the OAuth flow into discrete automatable steps using Playwright browser automation combined with Python OAuth client libraries. The process: (1) generate the auth URL programmatically using OAuth client libs, (2) navigate to the URL via Playwright controlled browser, (3) automate consent flow clicks through account selection and permission screens, (4) capture the authorization code from the localhost redirect, (5) exchange the code for tokens programmatically. Key insight: OAuth flows do not require run_local_server(); you can manually orchestrate each step. One gotcha discovered: brand channels vs personal channels. If you are an Editor (not Owner) on a YouTube brand channel, the mine=True API parameter will not return it; you must access via channel ID directly.
Result
Full YouTube Data API access provisioned programmatically in under 2 minutes (vs 20-30 min manual). Pattern reused for Gmail and Drive APIs. Key gotcha: brand channel editors must query by channel ID, not mine=True. Saved ~4 hours over 8 provisioning cycles.