- [C#] Hướng dẫn giải nén file *.rar với tiến trình progress bar winform
- [C#] Chia sẻ source code make Crazy Error Message Effect Bomb Windows
- [C#] Lập trình ứng dụng theo mô hình MVP Model-View-Presenter Pattern Winform
- [C#] Giới thiệu và những thứ hay ho từ thư viện System.Reactive của Microsoft
- [C#] Hướng dẫn tạo ứng dụng Chat với GPT sử dụng Open AI API
- [DEVEXPRESS] Tạo month picker trên DateEdit Winform C#
- [DATABASE] Cách sử dụng và lưu ý khi sử dụng khóa ngoại (Foreign Key) trong Sqlserver
- [C#] Garbage Collector (GC) là gì? Cách giải phóng bộ nhớ trên ứng dụng Winform khi các đối tượng không còn sử dụng
- [C#] Cách tính độ tương phản màu sắc Contrast Color mà con người có thể nhìn thấy được
- [C#] Hướng dẫn mã hóa mật khẩu tài khoản ứng dụng đúng chuẩn Men
- [C#] Sử dụng Open AI Chat GPT viết ứng dụng Count down timer có hiệu ứng trên winform
- [DATABASE] Chia sẻ dữ liệu Pantone Color sql và json api
- [SQLSERVER] Tạo mã sản phẩm tự động như: SP0001, SP0002, SP0003... sử dụng Trigger
- [C#] Hướng dẫn kiểm tra phiên bản NET Framework cài đặt ở máy tính
- [C#] Hướng dẫn đọc file excel đơn giản sử dụng thư viện Epplus
- [C#] ConcurrentBag là gì và cách sử dụng nó trong lập trình bất đồng bộ
- [C#] AutoResetEvent là gì và cách sử dụng
- [DEVEXPRESS] Chia sẻ source code cách tạo biểu đồ sơ đồ tổ chức công ty Org Chart trên Winform C#
- [C#] Hướng dẫn tạo Auto Number trên Datagridview winform
- [DATABASE] Hướng dẫn tạo Procedure String Split in Mysql
[C#] Hướng dẫn viết tools tự động upload video lên Youtube - using Api v3 Csharp
Bài viết hôm nay, mình sẽ tiếp tục hướng dẫn cho các bạn viết một tools nhỏ có chức năng tự động upload video của các bạn lên Youtube sử dụng Youtube API V3 của google cung cấp.
[C#] Tutorial Auto Upload Video Youtube using Youtube APi v3 csharp
Nếu bạn nào, thường làm việc hay upload video lên youtube, nếu các bạn upload ít video thì không sao, nếu các bạn upload nhiều video cùng một lúc thì lúc đó công việc của các bạn sẽ mất rất nhiều thời gian.
Giao diện demo ứng dụng:
Bài viết này, mình sẽ hướng dẫn các bạn cách upload video lên Youtube.
Đầu tiên, để sử dụng được Api Yoube v3, các bạn cần có một tài khoản gmail.
Sau đó, các bạn vào trang web
https://console.developers.google.com/apis/api?project=my-project-1480923364792&hl=EN
Để tạo một ứng dụng, như chúng ta thường làm việc với ứng dụng với facebook vậy.
Sau khi các bạn tạo thành công ứng dụng Google sẽ cung cấp cho chúng ta một: app_id, và một sercert_id. Và chúng ta sẽ sử dụng 2 khóa này để upload video lên youtube.
+ Sau khi tạo ứng dụng xong các bạn vào nuget để download và cài đặt packet Yoube.api.v3 vào project của mình.
Và dưới đây, là link code mẫu mà google có cung cấp cho chúng ta. Chúng ta chỉ cần đọc và tùy chỉnh lại thôi, các bạn có thể tham khảo ở link bên dưới:
https://developers.google.com/youtube/v3/code_samples/dotnet
Mình sẽ cung cấp source code cho các bạn bên dưới, các bạn chỉ cần tùy chỉnh lại các thông tin như:
- Đường dẫn file
- Tiêu đề video (title)
- Mô tả video (description)
- Tag Seo
- Thumbnail Video...
Dưới đây là source code:
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 System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace YoutubeUploadApi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
lblstatus.Text = "Đang upload video";
try
{
Thread thead = new Thread(() =>
{
Run().Wait();
});
thead.IsBackground = true;
thead.Start();
}
catch (AggregateException ex)
{
MessageBox.Show(ex.Message);
}
}
private async Task Run()
{
UserCredential credential;
using (var stream = new FileStream("client_id.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
// This OAuth 2.0 access scope allows an application to upload files to the
// authenticated user's YouTube channel, but doesn't allow other types of access.
new[] { YouTubeService.Scope.YoutubeUpload },
"user",
CancellationToken.None
);
}
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
});
var video = new Video();
video.Snippet = new VideoSnippet();
video.Snippet.Title = txtTieude.Text;
video.Snippet.Description = txtMota.Text;
string[] tagSeo = Regex.Split(txtTagSeo.Text, ",");d
video.Snippet.Tags = tagSeo;
video.Snippet.CategoryId = "22"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
video.Status = new VideoStatus();
video.Status.PrivacyStatus = "public"; // or "private" or "public"
var filePath = txtPath.Text; // Replace with path to actual movie file.
using (var fileStream = new FileStream(filePath, FileMode.Open))
{
var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;
await videosInsertRequest.UploadAsync();
}
}
void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
{
switch (progress.Status)
{
case UploadStatus.Uploading:
lblstatus.Text = String.Format("{0} bytes sent.", progress.BytesSent);
break;
case UploadStatus.Failed:
lblstatus.Text = String.Format("An error prevented the upload from completing.
{0}", progress.Exception);
break;
}
}
void videosInsertRequest_ResponseReceived(Video video)
{
lblstatus.Text = string.Format("Video id '{0}' was successfully uploaded.", video.Id);
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
if(open.ShowDialog() == DialogResult.OK)
{
txtPath.Text = open.FileName;
}
}
}
}
Video demo ứng dụng upload video via Youtube Api v3:
Have fun :)