Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Google日历API C凭据引发异常_C#_.net_Console Application_Google Calendar Api - Fatal编程技术网

C# Google日历API C凭据引发异常

C# Google日历API C凭据引发异常,c#,.net,console-application,google-calendar-api,C#,.net,Console Application,Google Calendar Api,我将遵循本教程: 我的代码: 问题是,这部分代码引发了一个异常: {无法加载文件或程序集 'Microsoft.Threading.Tasks.Extensions.Desktop,版本=1.0.16.0, 区域性=中性,PublicKeyToken=b03f5f7f11d50a3a'或其 依赖项。系统找不到该文件 指定:Microsoft.Threading.Tasks.Extensions.Desktop, 版本=1.0.16.0,区域性=中性,PublicKeyToken=b03f5f7f

我将遵循本教程: 我的代码:

问题是,这部分代码引发了一个异常:

{无法加载文件或程序集 'Microsoft.Threading.Tasks.Extensions.Desktop,版本=1.0.16.0, 区域性=中性,PublicKeyToken=b03f5f7f11d50a3a'或其 依赖项。系统找不到该文件 指定:Microsoft.Threading.Tasks.Extensions.Desktop, 版本=1.0.16.0,区域性=中性,PublicKeyToken=b03f5f7f11d50a3a}

注:虽然很可能与这个问题无关,但当在google上设置我的项目以获取凭据时,我选择了Installed Application->Other,因为我假设这就是控制台应用程序

编辑:添加似乎解决了这个问题。现在我剩下的代码是:

 var service = new CalendarService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = "Calendar API Test"
    });

    var x = service.Events.List("actualcalendarid").OauthToken;

    Console.WriteLine(x);
    Console.ReadLine();

返回一个空行,即使当我运行应用程序时,它确实请求访问我的日历等。我忘了什么吗?

确保您已将所有引用添加到项目中

需要使用NuGet安装以下内容

-Install-Package Google.Apis.Calendar.v3 –Pre
-Install-Package Google.Apis -Pre
-Install-Package DotNetOpenAuth -Version 4.3.4.13329
-Install-Package Google.Apis.Auth.Mvc -Pre
并将引用Microsoft.Threading.Tasks.Extensions添加到项目中

编辑: 您没有检索数据,因为您没有执行查询

var result = service.Events.List("primary").Execute();


    private async Task<UserCredential> GetCredential()
    {
        UserCredential credential = null;
        try
        {
            using(var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
            {
                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    new[] { CalendarService.Scope.Calendar },
                    "user", CancellationToken.None, new FileDataStore(""));
            }
        }
        catch (IOException ioe)
        {
            Debug.WriteLine("IOException" + ioe.Message);
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Exception" + ex);
        }
        return credential;
    }

    private CalendarService GetCalenderService(UserCredential credential)
    {
        CalendarService service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "Calendar Sample"
            });
        return service;
    }

    private Events GetEvent(string CalendarId)
    {
        var query = this.Service.Events.List(CalendarId);
        query.ShowDeleted = false;
        query.SingleEvents = true;
        return query.Execute();
    }

添加似乎解决了这个问题。现在还有另一个问题,尽管我已经编辑了我的post,但您是否可以尝试service.Events.Listprimary.Execute;恐怕没有这样的执行方法,isI已经用我正在使用的代码更新了我的答案。
var result = service.Events.List("primary").Execute();


    private async Task<UserCredential> GetCredential()
    {
        UserCredential credential = null;
        try
        {
            using(var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
            {
                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    new[] { CalendarService.Scope.Calendar },
                    "user", CancellationToken.None, new FileDataStore(""));
            }
        }
        catch (IOException ioe)
        {
            Debug.WriteLine("IOException" + ioe.Message);
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Exception" + ex);
        }
        return credential;
    }

    private CalendarService GetCalenderService(UserCredential credential)
    {
        CalendarService service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "Calendar Sample"
            });
        return service;
    }

    private Events GetEvent(string CalendarId)
    {
        var query = this.Service.Events.List(CalendarId);
        query.ShowDeleted = false;
        query.SingleEvents = true;
        return query.Execute();
    }