Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

fix(youtube): ask Google only for YouTube's scopes when verifying a channel

spacesdrive98a6ed976c94
diff --git a/backend/services/youtube/googleApi.ts b/backend/services/youtube/googleApi.ts
index a610a94..a0c1648 100644
--- a/backend/services/youtube/googleApi.ts
+++ b/backend/services/youtube/googleApi.ts
@@ -57,7 +57,7 @@ export async function authorizationUrl(config: YouTubeOAuthConfig, userId: strin
// offline and consent make Google return a refresh token every time, so a reconnect always gets one
access_type: 'offline',
prompt: 'consent',
- include_granted_scopes: 'true',
+ // no include_granted_scopes: sign-in scopes merged from the same client make Google refuse a Brand Account channel
// the state names the channel card the sign-in was started from, so completing it can check Google returned that channel
state: await createOAuthState(userId, config.clientSecret, accountId),
});
diff --git a/backend/tests/routes/youtubeRoutes.test.ts b/backend/tests/routes/youtubeRoutes.test.ts
index 399d6a9..31a0a36 100644
--- a/backend/tests/routes/youtubeRoutes.test.ts
+++ b/backend/tests/routes/youtubeRoutes.test.ts
@@ -44,6 +44,9 @@ test('a verify link is only made for one of the person\'s own YouTube channels,
assert.equal((await server.request('/auth/youtube/url?accountId=theirs', {}, ENV)).status, 404);
const made = await server.request('/auth/youtube/url?accountId=yt1', {}, ENV);
assert.equal(made.status, 200);
- const state = new URL(((await made.json()) as { url: string }).url).searchParams.get('state') ?? '';
- assert.deepEqual(await readOAuthState(state, 'google-secret'), { userId: 'user-1', subject: 'yt1' });
+ const params = new URL(((await made.json()) as { url: string }).url).searchParams;
+ assert.deepEqual(await readOAuthState(params.get('state') ?? '', 'google-secret'), { userId: 'user-1', subject: 'yt1' });
+ // only YouTube's scopes, never merged with earlier grants: a Brand Account channel refuses any other scope
+ assert.equal(params.get('scope'), 'https://www.googleapis.com/auth/youtube.upload https://www.googleapis.com/auth/youtube.readonly');
+ assert.equal(params.has('include_granted_scopes'), false);
});
diff --git a/docs/platform-v2/YOUTUBE_INTEGRATION.md b/docs/platform-v2/YOUTUBE_INTEGRATION.md
index 1b628fa..c1de659 100644
--- a/docs/platform-v2/YOUTUBE_INTEGRATION.md
+++ b/docs/platform-v2/YOUTUBE_INTEGRATION.md
@@ -25,6 +25,7 @@ How Twiligent uploads videos and Shorts to a person's YouTube channel (#147): th
1. `GET /api/auth/youtube/url?accountId=<id>` (signed in, for one of the person's own YouTube accounts) returns Google's consent URL:
- scopes `youtube.upload` and `youtube.readonly`;
- `access_type=offline` and `prompt=consent`, so a refresh token comes back every time;
+ - no `include_granted_scopes`: the instance may reuse a client that also signs people in with Google, and merging those sign-in scopes (`openid`, `email`, `profile`) makes Google refuse a Brand Account channel, which accepts YouTube scopes only (#173);
- a state signed with the Google client secret that names the person and the account (`createOAuthState` with a subject).
2. Google sends the browser to the public `GET /api/auth/youtube/callback`.
- It checks the state's signature and redirects to the Accounts page with `#yt_code=...&yt_state=...` in the fragment, which never reaches a server or a referrer.
diff --git a/site/content/docs/self-host/youtube-publishing.mdx b/site/content/docs/self-host/youtube-publishing.mdx
index 757b62f..93e9897 100644
--- a/site/content/docs/self-host/youtube-publishing.mdx
+++ b/site/content/docs/self-host/youtube-publishing.mdx
@@ -58,3 +58,10 @@ The video goes to your Cloudinary account from the browser first. Every 15 minut
- **Size:** your Cloudinary plan sets the largest video (100 MB on the free plan).
- **Daily limits:** Google allows 100 uploads a day per project, shared by every channel on your instance. When they run out, uploads continue after midnight Pacific time.
- **Thumbnails:** YouTube only accepts custom thumbnails from verified channels. Without one, the video is still published and the page says the thumbnail was not set.
+
+## Brand Account channels
+
+A channel that belongs to a Brand Account is verified the same way: choose **Verify** on its card, sign in with your own Google account, and pick the channel when Google asks which one to use.
+
+- **Who can verify:** an owner or manager of the Brand Account, listed at [myaccount.google.com/brandaccounts](https://myaccount.google.com/brandaccounts). Someone added only through YouTube Studio channel permissions cannot use YouTube's API for the channel.
+- **If Google shows an error page:** try again in a private window signed in to that one Google account. Google refuses a Brand Account sign-in that asks for anything but YouTube, and Twiligent asks only to upload and to read the channel.