Search code examples
c#youtube-apiyoutube-data-apidesktop-applicationgoogle-api-dotnet-client

Reporting a YouTube Video through API from Desktop Application


I am currently working on a desktop application that requires the ability to report inappropriate YouTube videos using the YouTube API. Although I have reviewed the YouTube API documentation, I'm encountering challenges in understanding how to implement the process of programmatically reporting a video. I have come across an example code in a different programming language, but I'm struggling to locate an equivalent solution in C#.

I have referred to the YouTube API documentation, specifically the videos.reportAbuse endpoint, which seems to provide the functionality I need. However, the code example provided is in a different programming language, and I'm unable to find a similar example in C#.

How do I implement the reportAbuse endpoint in 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();

Now I am using YouTubeService and getting this error ":

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

I have downloaded the GoogleCredential json file.


Solution

  • This should get you started its my auth coded for YouTube. Just change the scope.

    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

    Remember you need to grab the Abuse reason. from 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

    This code will require installed app credentials file which has YouTube api enabled under library.

    User credential storage.

    GoogleWebAuthorizationBroker by default stores the users credentials in the %appData% folder on the machine its running on denoted by the user name which in the code above is set to "test".

    If you want another user to be able to login then you should just change the "test" to something else and it will prompt the new user.