本文介绍了如何使用 Google.Apis.YouTube.v3 和 C# 将视频上传到 youtube?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 C#
创建了 console
应用程序.它将upload
Video
从本地驱动器上传到youtube
.我使用 此链接在 google api 中创建了新应用.我还使用 nuget
安装了所有必需的 packages
.当我运行我的应用程序时,我收到错误消息拒绝访问",我找不到问题.
我在 Task Run()
方法中遇到错误.
使用系统;使用 System.IO;使用 System.Reflection;使用 System.Threading;使用 System.Threading.Tasks;使用 Google.Apis.Auth.OAuth2;使用 Google.Apis.Services;使用 Google.Apis.Upload;使用 Google.Apis.Util.Store;使用 Google.Apis.YouTube.v3;使用 Google.Apis.YouTube.v3.Data;命名空间 Google.Apis.YouTube.Samples{///<总结>///YouTube 数据 API v3 示例:创建播放列表.///依赖于 .NET v1.7.0 或更高版本的 Google API 客户端库.///见 https://developers.google/api-client-library/dotnet/get_started///</总结>内部类 PlaylistUpdates{[STAThread]静态无效主(字符串 [] args){Console.WriteLine("YouTube 数据 API:播放列表更新");Console.WriteLine("==================================");尝试{new PlaylistUpdates().Run().Wait();}捕获(聚合异常前){foreach (var e in ex.InnerExceptions){Console.WriteLine("错误:" + e.Message);}}Console.WriteLine("按任意键继续...");Console.ReadKey();}私有异步任务运行(){UserCredential 凭证;使用 (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read)){凭据 = 等待 GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,//此 OAuth 2.0 访问范围允许对//经过身份验证的用户帐户.new[] { YouTubeService.Scope.Youtube },用户",CancellationToken.None,新的 FileDataStore(this.GetType().ToString()));}var youtubeService = new YouTubeService(new BaseClientService.Initializer(){HttpClientInitializer = 凭证,ApplicationName = this.GetType().ToString()});//在授权用户的频道中创建一个新的私有播放列表.var newPlaylist = new Playlist();newPlaylist.Snippet = new PlaylistSnippet();newPlaylist.Snippet.Title = "测试播放列表";newPlaylist.Snippet.Description = "使用 YouTube API v3 创建的播放列表";newPlaylist.Status = new PlaylistStatus();newPlaylist.Status.PrivacyStatus = "public";newPlaylist = await youtubeService.Playlists.Insert(newPlaylist, "snippet,status").ExecuteAsync();//将视频添加到新创建的播放列表中.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();Console.WriteLine("播放列表项 id {0} 已添加到播放列表 id {1}.", newPlaylistItem.Id, newPlaylist.Id);}}}
我需要将user"参数传递给 gmail 用户名吗?
任何使用 C#(控制台/网络)的工作示例?
感谢帮助.
解决方案感谢@iedoc 的代码和答案
一个>
一个基本的 Web 项目示例
默认.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html><html xmlns="http://www.w3/1999/xhtml"><head runat="服务器"><title>测试</title>头部><身体><form id="form1" runat="server"><div><p><asp:TextBox runat="server" ID="videoName" placeholder="视频标题"/></p><p><asp:TextBox runat="server" ID="videoDesc" placeholder="视频描述"/></p><p><asp:FileUpload ID="videoUpload" runat="server"/></p><p><asp:Button id="saveDetails" Text="Update Chapel Group" runat="server" OnClick="saveDetails_click"/></p></表单>