我目前正在开发一个桌面应用程序,它需要能够使用YouTube API报告不适当的YouTube视频.尽管我已经查看了YouTube API文档,但我在理解如何实现以编程方式报告视频的过程方面遇到了挑战.我遇到过用不同编程语言编写的示例代码,但我正在努力寻找用C#编写的等价解决方案.

我已经提到了YouTube API documentation,特别是Videos.reportAbuse端点,它似乎提供了我需要的功能.然而,提供的代码示例是用不同的编程语言编写的,我找不到用C#编写的类似示例.

如何在C#中实现reportAbuse端点?

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();

现在我正在使用YouTubeService,并收到此错误":

‘从JSON或JSON参数创建凭据时出错.无法识别凭据类型.’"

我已经下载了GoogleCredential json文件.

推荐答案

这应该会让你开始它的我的身份验证编码为YouTube.只要改变范围就行了.

using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;

Console.WriteLine("Hello, World!");

const string pathToKeyFile = @"C:\Development\FreeLance\GoogleSamples\Credentials\credentials.json";

const string videoPath = "";

// scope of authorization needed from the user
var scopes = new[] { YouTubeService.Scope.Youtube };

// file to upload
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.FromFile(pathToKeyFile).Secrets,
    scopes,
    "test",
    CancellationToken.None).Result;

// Create the  Youtube service.
var service = new YouTubeService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = "Daimto Youtube upload Quickstart"
});

Reasons list

记住,你需要抓住虐待的原因.从videoAbuseReportReasons list

{
  "items": [
    {
      "kind": "youtube#videoAbuseReportReason",
      "etag": "95t7kDR9fIaw7_qtu0LG4hkmfRI",
      "id": "N",
      "snippet": {
        "label": "Sex or nudity"
      }
    },
    {
      "kind": "youtube#videoAbuseReportReason",
      "etag": "rqXGFg-vDu_OHLwyfWecptaHny0",
      "id": "V",
      "snippet": {
        "label": "Violent, hateful, or dangerous",
        "secondaryReasons": [
          {
            "id": "35",
            "label": "Promotes violence or hatred"
          },
          {
            "id": "38",
            "label": "Suicide or self injury"
          },
          {
            "id": "39",
            "label": "Pharmaceutical or drug abuse"
          },
          {
            "id": "41",
            "label": "Graphic violence"
          },
          {
            "id": "43",
            "label": "Weapons"
          },
          {
            "id": "44",
            "label": "Digital security"
          },
          {
            "id": "40",
            "label": "Other violent, hateful, or dangerous acts"
          }
        ]
      }
    },
    {
      "kind": "youtube#videoAbuseReportReason",
      "etag": "orwADIA7_Uir4yYCOkr8aUPgKBc",
      "id": "C",
      "snippet": {
        "label": "Child abuse"
      }
    },
    {
      "kind": "youtube#videoAbuseReportReason",
      "etag": "_C6P5gRJeQg_3tZrpcwodoPmhMs",
      "id": "M",
      "snippet": {
        "label": "Medical misinformation"
      }
    },
    {
      "kind": "youtube#videoAbuseReportReason",
      "etag": "utqOkIhK33vDq9A64XcflehWnzg",
      "id": "E",
      "snippet": {
        "label": "Violent extremism"
      }
    },
    {
      "kind": "youtube#videoAbuseReportReason",
      "etag": "HWSFj3k4pMpiUDkp419CS3Ijkhc",
      "id": "H",
      "snippet": {
        "label": "Harassment or bullying"
      }
    }
  ],
  "kind": "youtube#videoAbuseReportReasonListResponse",
  "etag": "NHfccB0wL0fbMxeiDWA-XX_3NnY"
}

note

此代码将需要installed app个凭据文件,该文件在库下启用了YouTube API.

Csharp相关问答推荐

在Linq中调用需要limit和offset的方法''''

C#中使用BouncyCastle计算CMac

在多对多关系上不删除实体

在发布表单时绑定包含附加(嵌套)列表的对象列表的正确语法是什么

AsNoTrackingWithIdentitySolutions()似乎不起作用?

在具有主构造函数的类中初始化属性时出现警告

模型绑定RazorPage表单

ASP.NET配置kestrel以使用Windows证书存储中的HTTPS

在具有不同属性名称的两个类之间创建关系

用C#从Word文档中删除重复的节控件和文本内容控件

HttpClient 415不支持的媒体类型错误

Blazor Web App WASM的两个独立项目令人困惑

DbContext-传递自定义配置选项

有没有类似于扩展元素的合并元组的语法?

RabbitMQ群集的MassTransit配置问题

try 链接被委派者(多播委托)时,无法将获取运算符应用于类型为';方法组&39;和方法组';的操作数

在C#中有没有办法减少大型数组中新字符串的分配?

在集成测试中可以在模拟InMemory数据库中设定数据种子

用于获取字符串的最后12个字符的正则表达式(空格除外)

Maui:更改代码中的绑定字符串不会更新UI,除非重新生成字符串(MVVM)