【问题标题】:Inserting a comment into Youtube live stream chat with C# form app and youtube-v3-api使用 C# 表单应用程序和 youtube-v3-api 在 Youtube 直播聊天中插入评论
【发布时间】:2019-02-02 08:10:40
【问题描述】:

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

这是我的代码:

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# youtube youtube-api youtube-data-api


    【解决方案1】:

    我搞定了,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();
    

    【讨论】:

      猜你喜欢
      • 2015-08-23
      • 2017-11-29
      • 2018-10-28
      • 2015-03-03
      • 2018-03-19
      • 2016-09-07
      • 2018-04-14
      • 2016-04-06
      • 2021-02-27
      相关资源
      最近更新 更多