gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:13:51 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com