YouTube video works in browser but fails inside Flutter app with error 152-4

2 weeks ago 10
ARTICLE AD BOX

The error 152-4 (and its cousins 150/153) is a specific YouTube IFrame API rejection. It occurs when YouTube’s security system cannot verify the "Origin" of the request.

In a standard browser, the "Referer" header is automatically sent. In a Flutter WebView (which these packages use), the header is often missing or set to a local/invalid scheme (like data: or file:), causing YouTube to block the video—even if it's unlisted and has embedding enabled.

1. Why it works in Chrome but fails in Flutter

Browsers are "trusted origins" by default. When you use Youtubeer_iframe, you are essentially running a tiny web browser inside your app. If that internal browser doesn't tell YouTube, "Hey, I'm a legitimate request from [YourDomain].com," YouTube assumes it's a bot or an unauthorized scrape and triggers the 152-4 "Video Unavailable" safety block.

2. The Solution: Explicitly Set the Origin

The most reliable fix is to manually provide a valid origin parameter in your controller. This tricks the YouTube API into accepting the request as a standard web embed.

Update your initialization code like this:

Dart

_controller = YoutubePlayerController( params: const YoutubePlayerParams( autoPlay: false, showFullscreenButton: true, // ADD THIS LINE: origin: 'https://www.youtube.com' // OR: 'https://www.youtube-nocookie.com' ), )..loadVideoById(videoId: videoId);

[!TIP]

If https://www.youtube.com doesn't work, try using your actual website domain (e.g., https://yourcompany.com). Even if the app isn't a website, the IFrame API just needs a valid-looking https string to satisfy the security check.

3. Recent Policy Changes

You aren't imagining things—this has become more common recently. As of mid-2025, YouTube significantly tightened its "Embedder Identity Verification." They now enforce stricter checks on the Referer and Origin headers to combat ad-blockers and unauthorized third-party players.

4. Checklist for Android 12-14

Since you are testing on modern Android versions, check these two things:

Hardware Acceleration: Ensure hardware acceleration is enabled in your AndroidManifest.xml (it's on by default, but double-check if you've modified the <application> tag).

Clear WebView Cache: Sometimes the "Video Unavailable" screen gets "stuck" in the WebView's internal cache. Uninstall the app from your Moto G60 and do a fresh install after applying the origin fix.

Read Entire Article