执行请求失败https //www.google.com/calendar/feeds/default/allcalendars/full

时间:2014-12-18 10:42:16

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

我正在尝试下面的代码,但它总是给我同样的例外......

使用Google calender api v3

using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Calendar;

Google.GData.Calendar.CalendarService calendarService = new Google.GData.Calendar.CalendarService("App name");
        calendarService.setUserCredentials("username@gmail.com", "password");

        CalendarQuery query = new CalendarQuery();
        query.Uri = new Uri("https://www.google.com/calendar/feeds/default/allcalendars/full");
        CalendarFeed resultFeed = (CalendarFeed)calendarService.Query(query);
        Console.WriteLine("Your calendars:\n");
        foreach (CalendarEntry entry in resultFeed.Entries)
        {
            Console.WriteLine(entry.Title.Text + "\n");
        }

帮帮我!

1 个答案:

答案 0 :(得分:1)

要访问Google日历,您需要使用Oauth2进行身份验证,据我所知,Google Calendar API v3不允许客户端登录。

更新:从Google Calendar API documentation

  

关于授权协议

     

您的申请必须使用OAuth 2.0来授权请求​​。没有其他   支持授权协议。如果您的应用程序使用   Google+登录时,会为您处理授权的某些方面。

以下是有关如何创建经过身份验证的日历服务的简单示例。代码是从Google Calendar api C# authentication教程

中删除的
 string clientId = "";//From Google Developer console https://console.developers.google.com
 string clientSecret = "";//From Google Developer console https://console.developers.google.com
 string userName = ""//  A string used to identify a user.
 string[] scopes = new string[] {
    CalendarService.Scope.Calendar, // Manage your calendars
    CalendarService.Scope.CalendarReadonly // View your Calendars
 };

// 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;

// Create the service.
    CalendarService service = new CalendarService(new BaseClientService.Initializer() {
        HttpClientInitializer = credential,
        ApplicationName = "Calendar API Sample",
    });