I have created console
application using C#
.
Which will upload
Video
from local drive to youtube
.
I have created new app in google api using this link.
I have also installed all required packages
using nuget
.
When I run my application I am getting error as "Access Denied" I am not able to find the issue.
I am getting error in Task Run()
method.
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);
}
}
}
Do I need to pass 'user' parameter the gmail username?
Any working example using C# (console/web) ?
Help Appreciated.
I have made some minor changes to your code, its best to get your initial example of Oauth2 working first. Its hard to tell why you are getting an access denied. Here is what i have changed.
Info on the user parameter it is just used to create the credentials fine in the directory submitted to FileDataStore.
My login user name is
lilaw
in this manner you can have credentials files for each user. Being that this is a console application that doesn't matter much.What you should check if this does not work out of the box:
Tip: If you want to log out the current user or force it to login again. Just change Environment.UserName to something else it will force it to login again
I have tested this code it works:
Once you have that working there are several examples here one is for upload video
The following code sample calls the API's playlistItems.list method to retrieve a list of videos uploaded to the channel associated with the request. The code also calls the channels.list method with the mine parameter set to true to retrieve the playlist ID that identifies the channel's uploaded videos.
All Credit to @iedoc for his code and answer here
A basic working Web project example
Default.aspx
Default.aspx.cs
I hate .NET and I have been fighting with this for weeks. Some of the problems I encountered;
I would like to improve this code to provide actual upload status/feedback but I think that would need to be done client side. I am not very good at C#/.NET
UPDATE here is my feedback code via server & client side - https://stackoverflow.com/a/49516167/3790921