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

时间:2013-11-19 14:45:32

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

我四处搜索,相关文档已过期(此处的页面显示了早期版本的Google .net apis的示例: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。

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

这应该有效

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

答案 1 :(得分:-1)

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

参考此链接......它对我有用..

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

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