Flutter应用接入Google登录后获取Youtube播放列表报404问题求助
问题原因
你遇到的DetailedApiRequestError(status: 404, message: Channel not found)报错核心原因是:你的Google账号绑定了多个Youtube频道,移动端google_sign_in库默认不会触发官方的频道选择弹窗,API请求mine: true参数无法定位到具体要查询的Youtube频道,进而返回资源不存在错误。浏览器端官方测试页自带频道选择流程,因此可以正常调用。
修复步骤
1. 精简授权范围
你当前申请的权限过多,多余权限只会降低授权通过率,甚至触发谷歌额外审核,仅保留必要的播放列表只读权限即可:
final _googleSignIn = GoogleSignIn( scopes: [ // 仅保留Youtube只读权限即可获取私有播放列表 YouTubeApi.youtubeReadonlyScope, // 如果你确实需要联系人权限再保留,否则可以删除 PeopleServiceApi.contactsReadonlyScope, ], );
2. 新增频道选择流程
登录成功后先查询当前账号关联的所有Youtube频道,可自行实现弹窗让用户选择目标频道,再用对应频道ID查询播放列表,不要直接依赖mine: true参数:
Future<void> _handleSignIn() async { final _googleSignIn = GoogleSignIn( scopes: [ YouTubeApi.youtubeReadonlyScope, ], ); // 测试阶段可以保留,正式环境建议移除,避免每次都要求用户重新登录 await _googleSignIn.signOut(); await _googleSignIn.signIn(); final httpClient = await _googleSignIn.authenticatedClient(); if (httpClient == null) { // 授权失败处理 return; } final YouTubeApi youtubeApi = YouTubeApi(httpClient); // 第一步:先获取当前账号关联的所有Youtube频道 final channelResponse = await youtubeApi.channels.list( ["id", "snippet"], mine: true, ); if (channelResponse.items == null || channelResponse.items!.isEmpty) { print("当前账号未绑定任何Youtube频道"); return; } // 有多个频道时可自行实现选择弹窗,此处示例取第一个频道 final targetChannel = channelResponse.items!.first; print("选中频道:${targetChannel.snippet?.title}"); // 第二步:用选中的频道ID查询播放列表 final playlistResponse = await youtubeApi.playlists.list( ["snippet", "status"], channelId: targetChannel.id, maxResults: 25, ); print("播放列表数量:${playlistResponse.items?.length ?? 0}"); playlistResponse.items?.forEach((playlist) { print("播放列表名称:${playlist.snippet?.title}"); }); }
3. 检查谷歌云控制台配置
- 确认你的谷歌云项目已启用YouTube Data API v3
- 移动端OAuth 2.0客户端配置正确:Android端要填写正确的SHA1签名和包名,iOS端要填写正确的Bundle ID
- 如果你还在测试阶段,要在OAuth同意屏幕页面把测试账号添加到测试用户列表中,否则授权会失败
内容的提问来源于stack exchange,提问作者Supabase Enjoyer
相关产品推荐
相关产品推荐

