我想从我的应用程序中使用Calendar API,为此我创建了客户端,但它只在localhost中工作,它不能在服务器上运行。我按照Google Calendar API dotnet中的步骤进行操作。
你可以帮我解决这个问题。
代码就像。
public List<CalendarEvent> GetEvents(long clientId)
{
UserCredential credential;
string pathResponse = System.Web.HttpContext.Current.Server.MapPath("../GoogleApiResponses/calendarRes_" + clientId + ".json");
using (var stream =
new FileStream(path, FileMode.Open, FileAccess.Read))
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
_scopes,
"user",
CancellationToken.None,
new FileDataStore(pathResponse, true)).Result;
}
// Create Google Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = _applicationName,
});
// Define parameters of request.
EventsResource.ListRequest request = service.Events.List("primary");
request.TimeMin = DateTime.Now;
request.ShowDeleted = false;
request.SingleEvents = true;
request.MaxResults = 10;
request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
// List events.
Events events = request.Execute();
Console.WriteLine("Upcoming events:");
if (events.Items != null && events.Items.Count > 0)
{
var eventTrim = events.Items.Select(GetEventTrim).ToList();
//events.Items[0].Id
return eventTrim;
}
else
{
return new List<CalendarEvent>();
}
}