Google日历身份验证错误

时间:2012-05-31 08:26:34

标签: c# .net authentication google-calendar-api

我正在尝试通过最新的Google Data API SDK更新Google日历。

我的示例代码如下:

            string sGoogleUserName = "xxxx@gmail.com";
            string sGooglePassword = "xxxxxxxx";
            Uri oCalendarUri = new Uri("http://www.google.com/calendar/feeds/" +
                                       sGoogleUserName + "/private/full");

            //Initialize Calendar Service
            CalendarService oCalendarService = new CalendarService("CalendarSampleApp");
            oCalendarService.setUserCredentials(sGoogleUserName, sGooglePassword);

            //Use Proxy 
            GDataRequestFactory oRequestFactory =
                (GDataRequestFactory)oCalendarService.RequestFactory;
            WebProxy oWebProxy = new WebProxy(
                 WebRequest.DefaultWebProxy.GetProxy(oCalendarUri));
            oWebProxy.Credentials = CredentialCache.DefaultCredentials;
            oWebProxy.UseDefaultCredentials = true;
            oRequestFactory.Proxy = oWebProxy;


            //Set Event Entry 
            EventEntry oEventEntry = new EventEntry();
            oEventEntry.Title.Text = "Test Calendar Entry From .Net";
            oEventEntry.Content.Content =
              "Hurrah!!! I posted my first Google calendar event through .Net";

            //Set Event Location 
            Where oEventLocation = new Where();
            oEventLocation.ValueString = "New Zealand";
            oEventEntry.Locations.Add(oEventLocation);

            //Set Event Time
            When oEventTime = new When(new DateTime(2011, 5, 31, 9, 0, 0),
                 new DateTime(2011, 5, 31, 9, 0, 0).AddHours(1));
            oEventEntry.Times.Add(oEventTime);

            //Set Additional Properties
            ExtendedProperty oExtendedProperty = new ExtendedProperty();
            oExtendedProperty.Name = "SynchronizationID";
            oExtendedProperty.Value = Guid.NewGuid().ToString();
            oEventEntry.ExtensionElements.Add(oExtendedProperty);

            // CalendarService oCalendarService = GAuthenticate();

            //Prevents This Error
            //{"The remote server returned an error: (417) Expectation failed."}
            System.Net.ServicePointManager.Expect100Continue = false;

            //Save Event
            oCalendarService.Insert(oCalendarUri, oEventEntry);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

这是下面的错误

  

Google.GData.Client.GDataRequestException:执行身份验证   请求返回意外结果:405 at   Google.GData.Client.Utilities.getAuthException(TokenCollection令牌,   HttpWebResponse回复)   Google.GData.Client.Utilities.QueryClientLoginToken(GDataCredentials   gc,String serviceName,String applicationName,Boolean fUseKeepAlive,   IWebProxy proxyServer,Uri clientLoginHandler)at   Google.GData.Client.GDataGAuthRequest.QueryAuthToken(GDataCredentials   gc)at Google.GData.Client.GDataGAuthRequest.EnsureCredentials()at   Google.GData.Client.GDataRequest.EnsureWebRequest()at   Google.GData.Client.GDataGAuthRequest.EnsureWebRequest()at   Google.GData.Client.GDataGAuthRequest.CopyRequestData()at   Google.GData.Client.GDataGAuthRequest.Execute(Int32 retryCounter)at   Google.GData.Client.GDataGAuthRequest.Execute()at   Google.GData.Client.Service.EntrySend(Uri feedUri,AtomBase baseEntry,   GDataRequestType类型,AsyncSendData数据)at   Google.GData.Client.Service.Insert(Uri feedUri,AtomEntry newEntry,   AsyncSendData数据)在Google.GData.Client.Service.Insert [TEntry](Uri   feedUri,TEntry entry)

2 个答案:

答案 0 :(得分:3)

今天我使用你在网上找到的相同示例代码来点击这个。

通过一些测试,我发现如果我设置了一个HTTP代理(我使用的是查尔斯,但其他常见的是Fiddler等),它会起作用,但如果没有代理,我就会尝试。

在预感中,我在//使用代理服务器下注释掉了这些内容并且有效:

string googleUserName = "falagard@gmail.com";
string googlePassword = "winnie";
Uri calendarUri = GetGoogleCalendarUri();

//Initialize Calendar Service
CalendarService service = new CalendarService("AIConsole");
service.setUserCredentials(googleUserName, googlePassword);

//Use Proxy - NOTE COMMENTED THIS PART OUT
//GDataRequestFactory requestFactory = (GDataRequestFactory)service.RequestFactory;
//WebProxy proxy = new WebProxy(WebRequest.DefaultWebProxy.GetProxy(calendarUri));
//proxy.Credentials = CredentialCache.DefaultCredentials;
//proxy.UseDefaultCredentials = true;
//requestFactory.Proxy = proxy;

之后它就像一个魅力。

答案 1 :(得分:0)

再次使用3.5.12版本。也许你是代理人,需要设置 首先是正确的代理设置