You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#桌面应用调用YouTube API举报视频及认证错误解决

如何在C#中实现YouTube API的视频举报功能?

问题背景

我正在开发一款桌面应用,需要实现通过YouTube API举报不当YouTube视频的功能。查阅YouTube API文档后,仍难以理解编程实现视频举报的流程,找到过其他编程语言的示例,但没有对应的C#方案。确认videos.reportAbuse端点可满足需求,但缺少C#示例。

现有代码

string credentialsPath = "googlefile.json";

// Scopes for YouTube API
string[] scopes = { YouTubeService.Scope.YoutubeForceSsl };

// Load the credentials file
GoogleCredential credential;
using (var stream = new FileStream(credentialsPath, FileMode.Open, FileAccess.Read))
{
    credential = GoogleCredential.FromStream(stream).CreateScoped(scopes);
}

// Initialize the YouTube API service with OAuth2 credentials
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = "My Project 55555"
});

// Video ID you want to report
string videoId = "xxxxx";

// Create the abuse report snippet
var abuseReport = new VideoAbuseReport();
abuseReport.VideoId = videoId;
abuseReport.ReasonId = "spam"; // You need to find the appropriate reason ID

// Submit the report
var reportRequest = youtubeService.Videos.ReportAbuse(abuseReport);
var response = reportRequest.Execute();

遇到的错误

'Error creating credential from JSON or JSON parameters. Unrecognized credential type .'

已下载GoogleCredential json文件,但仍出现上述错误。


解决方案

1. 修正凭证加载方式

桌面应用不能直接用GoogleCredential.FromStream加载OAuth客户端ID JSON,需通过GoogleWebAuthorizationBroker完成用户授权流程,获取合法的用户凭证:

string credentialsPath = "googlefile.json"; // 此处为你在Google Cloud控制台下载的「桌面应用类型OAuth客户端ID」JSON文件
string[] scopes = { YouTubeService.Scope.YoutubeForceSsl };

UserCredential credential;
using (var stream = new FileStream(credentialsPath, FileMode.Open, FileAccess.Read))
{
    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.FromStream(stream).Secrets,
        scopes,
        "user",
        CancellationToken.None,
        new FileDataStore("YouTube.ReportAbuse")).Result;
}

首次运行时会弹出浏览器,引导用户完成YouTube账号授权,授权信息会保存在FileDataStore指定的本地目录,后续运行无需重复授权。

2. 验证ReasonId的合法性

ReasonId必须使用YouTube API定义的合法值,常见有效值包括:

  • spam
  • sexual_content
  • violent_content
  • hateful_content
  • harmful_content
  • copyright

3. 完善举报信息

建议补充Comments字段说明举报细节,提升审核效率:

var abuseReport = new VideoAbuseReport();
abuseReport.VideoId = "xxxxx"; // 目标视频ID
abuseReport.ReasonId = "spam";
abuseReport.Comments = "该视频大量发布垃圾广告内容,属于违规信息";

4. 完整修正后代码

using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
using System.IO;
using System.Threading;

string credentialsPath = "googlefile.json";
string[] scopes = { YouTubeService.Scope.YoutubeForceSsl };

// 加载并获取用户凭证
UserCredential credential;
using (var stream = new FileStream(credentialsPath, FileMode.Open, FileAccess.Read))
{
    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.FromStream(stream).Secrets,
        scopes,
        "user",
        CancellationToken.None,
        new FileDataStore("YouTube.ReportAbuse")).Result;
}

// 初始化YouTube服务
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = "My Project 55555"
});

// 创建举报请求
var abuseReport = new VideoAbuseReport();
abuseReport.VideoId = "xxxxx";
abuseReport.ReasonId = "spam";
abuseReport.Comments = "该视频存在垃圾信息违规";

// 提交举报
var reportRequest = youtubeService.Videos.ReportAbuse(abuseReport);
reportRequest.Execute();

内容的提问来源于stack exchange,提问作者Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 05:40:13