I added this class to form1:
public class YoutubeApiResponse
{
public string kind { get; set; }
public string etag { get; set; }
public string nextPageToken { get; set; }
public PageInfo pageInfo { get; set; }
public List<Item> items { get; set; }
}
The var Item not exist in the List
Then in form1 constructor I did:
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://www.googleapis.com/youtube/v3/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("search?q=manam&type=video&order=relevance&part=snippet&maxResults=50&key=AIzaSyA4GZFAgfvsAItwFRmnAZvbTHoDeyunf2k").Result;
if (response.IsSuccessStatusCode)
{
var youtubeResponse = await response.Content.ReadAsAsync < YoutubeApiResponse().Result();
}
}
The api key is not valid api key, I just changed some letters random.
And in this part i'm getting error on the line:
var youtubeResponse = await response.Content.ReadAsAsync < YoutubeApiResponse().Result();
YoutubeApiResponse not exist so I changed it to YoutubeApiResponse but this doesn't have the Result property.
Another error on this line: Error 2 The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.
And:
Error 1 'System.Net.Http.HttpContent' does not contain a definition for 'ReadAsAsync' and no extension method 'ReadAsAsync' accepting a first argument of type 'System.Net.Http.HttpContent' could be found (are you missing a using directive or an assembly reference?)
And the link that I'm using should be used to get the categories according to given region is: https://www.googleapis.com/youtube/v3/videoCategories?part=snippet®ionCode=IL&key=AIzaSyBgc1Gx-0tHbWp6-SWE0sQarpc6nGLtttt
In this example region code IL and api key.
I just not sure how to use it in my c# code. This is my form1 code:
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;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Net.Http;
using System.Net.Http.Headers;
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.GData.Client;
using Google.GData.Extensions;
using Google.GData.Extensions.MediaRss;
namespace Youtube_Manager
{
public partial class Form1 : Form
{
List<string> results = new List<string>();
string err1 = ""
string userName = "[email protected]";
public Form1()
{
InitializeComponent();
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://www.googleapis.com/youtube/v3/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("search?q=manam&type=video&order=relevance&part=snippet&maxResults=50&key=AIzaSyA4GZFAgfvsAItwFRmnAZvbTHoDeyunf2k").Result;
if (response.IsSuccessStatusCode)
{
var youtubeResponse = await response.Content.ReadAsAsync < YoutubeApiResponse().Result();
}
}
UserCredential credential;
using (FileStream stream = new FileStream(@"D:\C-Sharp\Youtube-Manager\Youtube-Manager\Youtube-Manager\bin\Debug\client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { YouTubeService.Scope.Youtube, YouTubeService.Scope.YoutubeUpload },
"user",
CancellationToken.None,
new FileDataStore("YouTube.Auth.Store")).Result;
}
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 = "Default Video Title";
video.Snippet.Description = "Default Video Description";
video.Snippet.Tags = new string[] { "tag1", "tag2" };
video.Snippet.CategoryId = "22";
video.Status = new VideoStatus();
video.Status.PrivacyStatus = "public";
var filePath = @"C:\Users\bout0_000\Videos\test.mp4";//@"E:\video.mp4";
using (var fileStream = new FileStream(filePath, FileMode.Open))
{
const int KB = 0x400;
var minimumChunkSize = 256 * KB;
var videosInsertRequest = youtubeService.Videos.Insert(video,
"snippet,status", fileStream, "video/*");
videosInsertRequest.ProgressChanged +=
videosInsertRequest_ProgressChanged;
videosInsertRequest.ResponseReceived +=
videosInsertRequest_ResponseReceived;
// The default chunk size is 10MB, here will use 1MB.
videosInsertRequest.ChunkSize = minimumChunkSize * 4;
videosInsertRequest.Upload();
}
}
private static void videosInsertRequest_ResponseReceived(Video obj)
{
}
private static void videosInsertRequest_ProgressChanged(IUploadProgress obj)
{
}
private async Task Run()
{
UserCredential credential = null;
using (var stream = new FileStream(@"D:\C-Sharp\Youtube-Manager\Youtube-Manager\Youtube-Manager\bin\Debug\client_secrets.json", FileMode.Open, FileAccess.Read))
{
try
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
// This OAuth 2.0 access scope allows for read-only access to the authenticated
// user's account, but not other types of account access.
new[] { YouTubeService.Scope.YoutubeReadonly },
"user",
CancellationToken.None,
new FileDataStore(this.GetType().ToString())
);
}
catch (Exception errrr)
{
err1 = errrr.ToString();
}
}
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = this.GetType().ToString()
});
var channelsListRequest = youtubeService.Channels.List("snippet,contentDetails");
channelsListRequest.ForUsername = userName;
var channelsListResponse = await channelsListRequest.ExecuteAsync();
foreach (var channel in channelsListResponse.Items)
{
var uploadsListId = channel.ContentDetails.RelatedPlaylists.Uploads;
var nextPageToken = "";
while (nextPageToken != null)
{
var playlistRequest = youtubeService.Playlists.List("id,snippet,contentDetails,status,player");
playlistRequest.Id = uploadsListId;
playlistRequest.MaxResults = 50;
playlistRequest.PageToken = nextPageToken;
var playlistListResponse = await playlistRequest.ExecuteAsync();
if (playlistListResponse.Items.Count > 0)
MessageBox.Show(playlistListResponse.Items[0].Snippet.Title);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
public class YoutubeApiResponse
{
public string kind { get; set; }
public string etag { get; set; }
public string nextPageToken { get; set; }
public PageInfo pageInfo { get; set; }
public List<Item> items { get; set; }
}
}
}
Using the part to get the categories according to given region id and add the categories later to a ComboBox as items that's i don't understand how to do.
Your mixing some things up.
Authenticate using a public API key:
Authenticate
Make a request:
code ripped from sample project Google-dotnet-samples Youtube
Update videoCatagories example: