gpt4 book ai didi

c# - 如何使用 Google.Apis.YouTube.v3 和 C# 将视频上传到 youtube?

转载 作者:可可西里 更新时间:2023-11-01 07:58:42 30 4
gpt4 key购买 nike

我已经使用 C# 创建了 console 应用程序。这将从本地驱动器上传 视频youtube。我使用 this link 在 google api 中创建了新应用程序.我还使用 nuget 安装了所有必需的 packages。当我运行我的应用程序时,出现“拒绝访问”错误,我找不到问题所在。

我在 Task Run() 方法中遇到错误。

using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

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 Google.Apis.YouTube.Samples
{
/// <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>
internal class PlaylistUpdates
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("YouTube Data API: Playlist Updates");
Console.WriteLine("==================================");

try
{
new PlaylistUpdates().Run().Wait();
}
catch (AggregateException ex)
{
foreach (var e in ex.InnerExceptions)
{
Console.WriteLine("Error: " + e.Message);
}
}

Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}

private 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()
});

// Create a new, private playlist in the authorized user's channel.
var newPlaylist = new Playlist();
newPlaylist.Snippet = new PlaylistSnippet();
newPlaylist.Snippet.Title = "Test Playlist";
newPlaylist.Snippet.Description = "A playlist created with the YouTube API v3";
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();

Console.WriteLine("Playlist item id {0} was added to playlist id {1}.", newPlaylistItem.Id, newPlaylist.Id);
}
}
}

我是否需要将“用户”参数传递给 gmail 用户名?

任何使用 C#(控制台/网络)的工作示例?

感谢帮助。

最佳答案

他的 code and answer here 归功于@iedoc

一个基本的工作 Web 项目示例

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>testing</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p><asp:TextBox runat="server" ID="videoName" placeholder="Video Title" /></p>
<p><asp:TextBox runat="server" ID="videoDesc" placeholder="Video Description" /></p>
<p><asp:FileUpload ID="videoUpload" runat="server" /></p>
<p><asp:Button id="saveDetails" Text="Update Chapel Group" runat="server" OnClick="saveDetails_click" /></p>
</div>
</form>
</body>
</html>

Default.aspx.cs

using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

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;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Responses;

public partial class _Default : System.Web.UI.Page
{
string vID = "none";
public void Page_Load(object sender, EventArgs e)
{

}
protected void saveDetails_click(object sender, EventArgs e)
{
if (Path.GetFileName(videoUpload.PostedFile.FileName) != "")
{
YouTubeUtilities ytU = new YouTubeUtilities("REFRESH", "SECRET", "CLIENT_ID"); // pass in your API codes here

using (var fileStream = videoUpload.PostedFile.InputStream) // the selected post file
{
vID = ytU.UploadVideo(fileStream,videoName.Text,videoDesc.Text,"22",false);
}
Response.Write(vID);
}

}
}
public class YouTubeUtilities
{
/*
Instructions to get refresh token:
* https://stackoverflow.com/questions/5850287/youtube-api-single-user-scenario-with-oauth-uploading-videos/8876027#8876027
*
* When getting client_id and client_secret, use installed application, other (this will make the token a long term token)
*/
private String CLIENT_ID { get; set; }
private String CLIENT_SECRET { get; set; }
private String REFRESH_TOKEN { get; set; }

private String UploadedVideoId { get; set; }

private YouTubeService youtube;

public YouTubeUtilities(String refresh_token, String client_secret, String client_id)
{
CLIENT_ID = client_id;
CLIENT_SECRET = client_secret;
REFRESH_TOKEN = refresh_token;

youtube = BuildService();
}

private YouTubeService BuildService()
{
ClientSecrets secrets = new ClientSecrets()
{
ClientId = CLIENT_ID,
ClientSecret = CLIENT_SECRET
};

var token = new TokenResponse { RefreshToken = REFRESH_TOKEN };
var credentials = new UserCredential(new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = secrets
}),
"user",
token);

var service = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credentials,
ApplicationName = "TestProject"
});

//service.HttpClient.Timeout = TimeSpan.FromSeconds(360); // Choose a timeout to your liking
return service;
}

public String UploadVideo(Stream stream, String title, String desc, String categoryId, Boolean isPublic)
{
var video = new Video();
video.Snippet = new VideoSnippet();
video.Snippet.Title = title;
video.Snippet.Description = desc;
video.Snippet.CategoryId = categoryId; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
video.Status = new VideoStatus();
video.Status.PrivacyStatus = isPublic ? "public" : "unlisted"; // "private" or "public" or unlisted

//var videosInsertRequest = youtube.Videos.Insert(video, "snippet,status", stream, "video/*");
var videosInsertRequest = youtube.Videos.Insert(video, "snippet,status", stream, "video/*");
videosInsertRequest.ProgressChanged += insertRequest_ProgressChanged;
videosInsertRequest.ResponseReceived += insertRequest_ResponseReceived;

videosInsertRequest.Upload();

return UploadedVideoId;
}

void insertRequest_ResponseReceived(Video video)
{
UploadedVideoId = video.Id;
// video.ID gives you the ID of the Youtube video.
// you can access the video from
// http://www.youtube.com/watch?v={video.ID}
}

void insertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
{
// You can handle several status messages here.
switch (progress.Status)
{
case UploadStatus.Failed:
UploadedVideoId = "FAILED";
break;
case UploadStatus.Completed:
break;
default:
break;
}
}
}

我讨厌 .NET,我已经为此奋斗了数周。我遇到的一些问题;

  1. 一对错误的 API key ,它们根本不起作用。我猜这是一个随机错误。
  2. 我还有一些从 Youtube 下载的 MP4 无法上传回来,在创作者工作室中他们会说“上传失败:无法处理文件”,我通过尝试在 youTube 界面上传它们来确定这一点。 (不是通过 API)
  3. 异步与同步给我带来了很多我不明白的问题。

我想改进此代码以提供实际的上传状态/反馈,但我认为这需要在客户端完成。我不太擅长 C#/.NET

更新 这是我通过服务器和客户端的反馈代码 - https://stackoverflow.com/a/49516167/3790921

关于c# - 如何使用 Google.Apis.YouTube.v3 和 C# 将视频上传到 youtube?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44593609/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com