Google日历API无法在服务器上运行

时间:2016-06-17 06:19:20

标签: c# google-api google-calendar-api google-oauth google-api-dotnet-client

我想从我的应用程序中使用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>();
        }
    }

1 个答案:

答案 0 :(得分:0)

您需要转到google developers console编辑凭据并添加服务器的重定向URI。

如果您阅读了灰色文本,它会告诉您这样做。

enter image description here