首页 > 解决方案 > 使用 C# 表单应用程序和 youtube-v3-api 在 Youtube 直播聊天中插入评论

问题描述

我正在尝试使用 Visual Studio 中的 C# 表单应用程序将评论插入到 youtube 直播聊天中。我使用了添加播放列表示例并正在对其进行修改以添加评论。我可以添加播放列表,因此身份验证正常。但是,我插入评论的代码什么也不做。我已将 client_secret.json 复制到我的旧项目并启用“每次复制”。我不明白为什么它不起作用,因为播放列表功能有效。我还将 API Explorer 与 LiveChatId 一起使用,它可以工作并向我的流添加评论,所以我知道我有正确的 LiveChatId 和片段信息。我不明白问题是什么。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Reflection;
using System.Threading;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;

namespace WindowsFormsApp19
{
class InsertComment
{

    /// <summary>
    /// YouTube Data API v3 sample: create a playlist.
    /// Relies on the Google APIs Client Library for .NET, v1.7.0 or higher.
    /// See https://developers.google.com/api-client-library/dotnet/get_started
    /// </summary>

    public async Task Run()
    {
        UserCredential credential;
        using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
        {
            credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                // This OAuth 2.0 access scope allows for full read/write access to the
                // authenticated user's account.
                new[] { YouTubeService.Scope.Youtube },
                "user",
                CancellationToken.None,
                new FileDataStore(this.GetType().ToString())
            );
        }

        var youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = this.GetType().ToString()
        }); 


        //PART THAT I ADDED THAT DOESN'T WORK
        var comments = new LiveChatMessage();
        comments.Snippet = new LiveChatMessageSnippet();
        comments.Snippet.LiveChatId = "Cg0KC3BabXB2fjJRRm1Z";
        comments.Snippet.Type = "textMessageEvent";
        comments.Snippet.TextMessageDetails.MessageText = "testing 456";
        comments = await youtubeService.LiveChatMessages.Insert(comments, "snippet").ExecuteAsync();


        //EVERYTHING BELOW HERE WORKS FINE
        //Create a new, private playlist in the authorized user's channel.

        var newPlaylist = new Playlist();
        newPlaylist.Snippet = new PlaylistSnippet();
        newPlaylist.Snippet.Title = "test";
        newPlaylist.Snippet.Description = "testsldkfja;sdlf";
        newPlaylist.Status = new PlaylistStatus();
        newPlaylist.Status.PrivacyStatus = "public";
        newPlaylist = await youtubeService.Playlists.Insert(newPlaylist, "snippet,status").ExecuteAsync();

        //Add a video to the newly created playlist.
        var newPlaylistItem = new PlaylistItem();
        newPlaylistItem.Snippet = new PlaylistItemSnippet();
        newPlaylistItem.Snippet.PlaylistId = newPlaylist.Id;
        newPlaylistItem.Snippet.ResourceId = new ResourceId();
        newPlaylistItem.Snippet.ResourceId.Kind = "youtube#video";
        newPlaylistItem.Snippet.ResourceId.VideoId = "GNRMeaz6QRI";
        newPlaylistItem = await youtubeService.PlaylistItems.Insert(newPlaylistItem, "snippet").ExecuteAsync();

    }
}

}

这是调用 InsertComment 类的类的代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp19
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    }
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
    }
    private void button2_Click(object sender, EventArgs e)
    {
        try
        { 
            new InsertComment().Run().Wait();
        }
        catch (AggregateException ex)
        {
              foreach (var f in ex.InnerExceptions)
            {
                   Console.WriteLine("Error: " + f.Message);
            }
        }
    }
}

}

标签: c#youtubeyoutube-apiyoutube-data-api

解决方案


我开始工作了,TextMessageDetails 为空,希望有人觉得这很有用..

LiveChatMessageSnippet mySnippet = new LiveChatMessageSnippet();
LiveChatMessage comments = new LiveChatMessage();
LiveChatTextMessageDetails txtDetails = new LiveChatTextMessageDetails();
txtDetails.MessageText = "yay";
mySnippet.TextMessageDetails = txtDetails;
mySnippet.LiveChatId = "Cg0KC3BabXB2ejfRRm1Z";
mySnippet.Type = "textMessageEvent";
comments.Snippet = mySnippet;
comments = await youtubeService.LiveChatMessages.Insert(comments, "snippet").ExecuteAsync();

推荐阅读