如何正确验证YouTube v3 Google API.NET的Google帐户



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

我正在尝试创建一个可恢复上传到YouTube的应用程序。我在谷歌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 v3api进行身份验证似乎没有明确的方法。

任何帮助都将不胜感激!

这应该能在中工作

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

相关内容

  • 没有找到相关文章

最新更新