I installed the google api 3 and the OAuth2. And i tried the google develope example:
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 Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.YouTube;
using Google.GData.Extensions.MediaRss;
using Google.YouTube;
namespace Youtube_Manager
{
public partial class Form1 : Form
{
string devKey = "mykey";
string userName = "my gmail as login";
string Password = "mypass";
string feedUrl = "https://gdata.youtube.com/feeds/api/users/default/playlists?v=2";
YouTubeRequestSettings settings;
public Form1()
{
InitializeComponent();
settings = new YouTubeRequestSettings("Youtube Uploader", devKey);
YouTubeRequest request = new YouTubeRequest(settings);
Feed<Video> videoFeed = request.Get<Video>(new Uri(feedUrl));
printVideoFeed(videoFeed);
}
static string auth;
static void printVideoFeed(Feed<Video> feed)
{
foreach (Video entry in feed.Entries)
{
auth = entry.Author;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
The first problem is that the link https://gdata.youtube.com/feeds/api/users/default/playlists?v=2 require loging and password i think when trying to browse to this link i'm getting:
User authentication required. Error 401
The second problem is i'm not sure i'm using the right key. My application name is Youtube Uploader so i went to the google console at: https://console.developers.google.com
There on the left i clicked on apis and enabled: youtube data api v3 And also enabled YouTube Analytics API
Then i clicked on Credentials and i created keys for OAuth so i have now client id key email address key and Certificate fingerprints
Then under it i created public api access and i have Api key.
Then i went to this site start with: https://code.google.com/apis/youtube/dashboard/gwt/index.html#product
And there i see my Developer Key and i didn't use it yet in my csharp code.
Now i want to do first thing just to get a list of my own videos i have uploaded to youtube using my today/current log in and password in this case under the name Daniel Lipman and when i log in i'm using my gmail [email protected] I have some videos i added some years ago.
So the problems are that the link require login and password. And not sure how to use my developer key username and password in my code.
I forgot to mention that only now i found the link with my developer key untill now i tried to use as devKey my client ID key and i guess i was wrong. Now i found my developer key long key.
EDIT
I tried now this:
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 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 devKey = "dev key";
string apiKey = "api key";
string userName = "my gmail address";
string Password = "pass";
public Form1()
{
InitializeComponent();
Run();
}
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 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())
);
}
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = this.GetType().ToString()
});
var channelsListRequest = youtubeService.Channels.List("contentDetails");
channelsListRequest.Mine = true;
// Retrieve the contentDetails part of the channel resource for the authenticated user's channel.
var channelsListResponse = await channelsListRequest.ExecuteAsync();
foreach (var channel in channelsListResponse.Items)
{
// From the API response, extract the playlist ID that identifies the list
// of videos uploaded to the authenticated user's channel.
var uploadsListId = channel.ContentDetails.RelatedPlaylists.Uploads;
Console.WriteLine("Videos in list {0}", uploadsListId);
var nextPageToken = "";
while (nextPageToken != null)
{
var playlistItemsListRequest = youtubeService.PlaylistItems.List("snippet");
playlistItemsListRequest.PlaylistId = uploadsListId;
playlistItemsListRequest.MaxResults = 50;
playlistItemsListRequest.PageToken = nextPageToken;
// Retrieve the list of videos uploaded to the authenticated user's channel.
var playlistItemsListResponse = await playlistItemsListRequest.ExecuteAsync();
foreach (var playlistItem in playlistItemsListResponse.Items)
{
// Print information about each video.
//Console.WriteLine("{0} ({1})", playlistItem.Snippet.Title, playlistItem.Snippet.ResourceId.VideoId);
results.Add(playlistItem.Snippet.Title);
}
nextPageToken = playlistItemsListResponse.NextPageToken;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
But it dosn't do anything not throwing exceptions or errors. I used a breakpoint on the line:
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
And right after this line nothing happen it's showing the form1 and that's it.
Now in the constructor i changed and did:
this.Run().Wait();
Now i'm getting exception on this line:
this.Run().Wait();
System.AggregateException was unhandled
HResult=-2146233088
Message=One or more errors occurred.
Source=mscorlib
StackTrace:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at Youtube_Manager.Form1..ctor() in d:\C-Sharp\Youtube-Manager\Youtube-Manager\Youtube-Manager\Form1.cs:line 43
at Youtube_Manager.Program.Main() in d:\C-Sharp\Youtube-Manager\Youtube-Manager\Youtube-Manager\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.IO.FileNotFoundException
HResult=-2147024894
Message=Could not find file 'D:\C-Sharp\Youtube-Manager\Youtube-Manager\Youtube-Manager\bin\Debug\client_secrets.json'.
Source=mscorlib
FileName=D:\C-Sharp\Youtube-Manager\Youtube-Manager\Youtube-Manager\bin\Debug\client_secrets.json
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
at Youtube_Manager.Form1.<Run>d__1.MoveNext() in d:\C-Sharp\Youtube-Manager\Youtube-Manager\Youtube-Manager\Form1.cs:line 51
InnerException:
Where do i find this file: client_secrets.json is this is the problem ?