如何正确validation适用于.NET的YouTube v3 Google API的Google帐户

我四处搜索,相关文档已过时(此处的页面显示了早期版本的Google .net apis的示例: https : //developers.google.com/youtube/v3/code_samples/dotnet )

我正在尝试创建一个可恢复上传到YouTube的应用程序。 我的应用程序已在Google API控制台上注册,并具有我的客户机密和客户端ID。 这是我用来validation的方法:

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")); } 

该过程在等待呼叫时挂起。 client_secrets.json文件加载正常(单独测试)。 但是,当调用AuthorizeAsync时,我会在挂起之前获得以下输出:

 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 

我完全不知道它正在寻找什么文件。 我看了几个其他的样本,我只是完全失去了如何做到这一点。 似乎没有明确的方法来validationYoutube 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; } 

https://code.google.com/p/google-api-dotnet-client/source/browse/?repo=samples#hg%2FTasks.ASP.NET.SimpleOAuth2

参考这个链接……它对我有用..

只需添加一个json文件与您的客户端ID和客户端密码,如:

{“web”:{“client_id”:“您的客户ID”“client_secret”:“您的客户秘密”}}

  GoogleAuthorizationCodeFlow flow; var assembly = Assembly.GetExecutingAssembly(); using (var stream = assembly.GetManifestResourceStream("Tasks.ASP.NET.SimpleOAuth2.client_secrets.json")) { flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer { DataStore = new FileDataStore("Tasks.ASP.NET.Sample.Store"), ClientSecretsStream = stream, Scopes = new[] { TasksService.Scope.TasksReadonly } }); } var uri = Request.Url.ToString(); var code = Request["code"]; if (code != null) { var token = flow.ExchangeCodeForTokenAsync(UserId, code, uri.Substring(0, uri.IndexOf("?")), CancellationToken.None).Result; // Extract the right state. var oauthState = AuthWebUtility.ExtracRedirectFromState( flow.DataStore, UserId, Request["state"]).Result; Response.Redirect(oauthState); } else { var result = new AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync(UserId, CancellationToken.None).Result; if (result.RedirectUri != null) { // Redirect the user to the authorization server. Response.Redirect(result.RedirectUri); } else { // The data store contains the user credential, so the user has been already authenticated. service = new TasksService(new BaseClientService.Initializer { ApplicationName = "Tasks API Sample", HttpClientInitializer = result.Credential }); } }