【问题标题】:How do I properly authenticate Google Account for YouTube v3 Google APIs for .NET如何正确验证 YouTube v3 Google APIs for .NET 的 Google 帐户
【发布时间】:2013-12-03 04:23:02
【问题描述】:

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

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

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

我完全不知道它在寻找什么文件。我查看了其他几个示例,但我完全不知道如何做到这一点。似乎没有明确的方法来使用 Youtube v3 api 进行身份验证。

任何帮助将不胜感激!

【问题讨论】:

  • 您能否检查我们示例存储库中的其他示例是否也不适用于您(您可以查看我们的图书示例,例如 code.google.com/p/google-api-dotnet-client/source/browse/…)。另外请确认以下补丁已经安装microsoft.com/en-us/download/details.aspx?id=3556
  • 原来我使用的是 .NET Framework 4.5 而不是 4 导致它崩溃。重新开始使用 .NET 4 解决了这个问题。谢谢。
  • 您可以使用 .NET 4.5 或 .NET 4.0。但不建议在安装完所有 NuGet 依赖后更改目标框架。
  • 我用 .NET 4 开始了一个新项目,而不是在第一个项目中更改框架。它显然在 .NET 4.5 中不起作用,至少在 Visual Studio 2012 中。可能应该看看,我知道它仍然是一个测试版。
  • 你是什么意思它在 .NET 4.5 上不起作用?如果您开始一个新项目并安装最新的 NuGet 版本的 Google.Apis.Youtube.v3,然后复制上面的核心,您会收到该错误吗?我不知道它不适用于 .NET 4.5。也许你需要安装一些补丁\服务包。检查您是否有最新版本的 4.5,如果没有更新它。谢谢!

标签: c# youtube-api google-api-dotnet-client


【解决方案1】:

这应该可以工作

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

【讨论】:

    【解决方案2】:

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

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

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

    { “网络”:{ "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
                    });
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-13
      • 2013-10-15
      • 1970-01-01
      • 2014-11-24
      • 1970-01-01
      • 2021-09-26
      • 1970-01-01
      • 2012-09-30
      相关资源
      最近更新 更多