如何正确验证YouTube v3的Google帐户.NET的Google API [英] How do I properly authenticate Google Account for YouTube v3 Google APIs for .NET
问题描述
我已经四处搜索,有关此文档的文档已过时(此处的页面显示了Google .net api早期版本的示例:
I've searched all around, and the documentation on this is out of date (the page located here show examples of earlier versions of the Google .net apis: https://developers.google.com/youtube/v3/code_samples/dotnet)
我正在尝试创建可恢复上传到YouTube的应用程序.我已经在Google API控制台上注册了我的应用程序,并拥有了我的客户机密和客户ID.这是我用来进行身份验证的方法:
I'm trying to create an application for resumable uploads to YouTube. I have my application registered on the Google APIs console and have my client secrets and client id. This is the approach I am using to authenticate:
UserCredential credential;
using (FileStream stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { YouTubeService.Scope.Youtube, YouTubeService.Scope.YoutubeUpload },
"[my_username]", CancellationToken.None,
new FileDataStore("YouTube.ListMyLibrary"));
}
该进程挂起了await调用. client_secrets.json文件正在很好地加载(对此进行了单独测试).但是,在调用AuthorizeAsync时,在挂起之前,我确实得到了以下输出:
The process hangs on the await call. The client_secrets.json file is loading just fine (tested this separately). When the AuthorizeAsync is called, however, I do get the following output before it hangs:
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in Microsoft.Threading.Tasks.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in Microsoft.Threading.Tasks.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
我完全不知道它在寻找什么文件.我查看了其他几个示例,但对如何执行此操作完全不知所措.似乎没有明确的方法可以通过Youtube v3 api进行身份验证.
I have absolutely no clue what files it is looking for. I have looked at several other samples and I'm just at a complete loss on how to do this. There seems to be no clear way to authenticate with the Youtube v3 api.
任何帮助将不胜感激!
推荐答案
这应该有效
UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open,
FileAccess.Read)) {
GoogleWebAuthorizationBroker.Folder = "Tasks.Auth.Store";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] {YouTubeService.Scope.Youtube, YouTubeService.Scope.YoutubeUpload },
"user",
CancellationToken.None,
new FileDataStore("YouTube.Auth.Store")).Result;
}
这篇关于如何正确验证YouTube v3的Google帐户.NET的Google API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!