c# - 无法创建 Google.Apis.Services.BaseClientService 的实例

标签 c# .net google-calendar-api google-api-dotnet-client

我正在按照本页中提到的说明编写代码:

https://developers.google.com/api-client-library/dotnet/get_started

按照上述资源中的这一行代码:-

// Create the service.
var service = new DiscoveryService(new BaseClientService.Initializer
    {
        ApplicationName = "Discovery Sample",
        APIKey="[YOUR_API_KEY_HERE]",
    });

我编写了以下代码来访问 Google 日历条目:-

CalendarListResource r = new CalendarListResource(new BaseClientService.Initializer
                {
                    ApiKey = "sdfsdfsdf",
                    ApplicationName = "Expenses"
                });

但是,我收到以下错误:-

cannot convert from 'Google.Apis.Services.BaseClientService.Initializer' to 'Google.Apis.Services.IClientService'

谁能帮我解决这个问题?

最佳答案

您需要使用 Oauth 才能访问 Google 日历。您这样做的方式仅适用于公共(public) API。

这是让 Oauth2 与 Google Calendar 一起工作的快速示例

/// <summary>
        /// Authenticate to Google Using Oauth2
        /// Documentation https://developers.google.com/accounts/docs/OAuth2
        /// </summary>
        /// <param name="clientId">From Google Developer console https://console.developers.google.com</param>
        /// <param name="clientSecret">From Google Developer console https://console.developers.google.com</param>
        /// <param name="userName">A string used to identify a user.</param>
        /// <returns></returns>
        public static CalendarService AuthenticateOauth(string clientId, string clientSecret, string userName)
        {

            string[] scopes = new string[] {
        CalendarService.Scope.Calendar  ,  // Manage your calendars
        CalendarService.Scope.CalendarReadonly    // View your Calendars
            };

            try
            {
                // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
                UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
                                                                    , scopes
                                                                    , userName
                                                                    , CancellationToken.None
                                                                    , new FileDataStore("Daimto.GoogleCalendar.Auth.Store")).Result;



                CalendarService service = new CalendarService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Calendar API Sample",
                });
                return service;
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.InnerException);
                return null;

            }

        }

关于c# - 无法创建 Google.Apis.Services.BaseClientService 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27358956/

相关文章:

c# - 对比 C# 泛型与 Haskell 参数化类型

c# - 为 .NET 应用程序创建交互式 shell,并将 python/iron python 等脚本语言嵌入其中

c# - C# 字典是否以与 Java HashMaps 相同的方式使用散列?

c# - 存储过程有时返回short,有时返回int

java - 我应该如何在 Gentics Mesh 中实现包含重复事件的日历?

javascript - 我的 FullCalendar 无法创建 Google 事件

c# - 在AudioSource的输入处生成音频

C# 任务忽略取消超时

c# - .Net中科学计算推荐的数据类型是什么?

google-api - 添加到 Google 日历时如何在项目描述中使用换行符