使用 C# 表单应用程序和 youtube-v3-api 在 Youtube 实时流聊天中插入评论



我正在尝试使用Visual Studio中的C#表单应用程序在youtube直播聊天中插入评论。我使用了添加播放列表示例,并正在对其进行修改以添加评论。我可以添加播放列表,因此身份验证正常工作。但是,我插入注释的代码没有任何作用。我已将 client_secret.json 复制到我的项目的旧项目并启用了"每次复制"。我不明白为什么它不起作用,因为播放列表功能有效。我还将 API 资源管理器与 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);
}
}
}
}

}

我让它工作了,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();

最新更新