0

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

我的示例代码如下:

            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:身份验证请求的执行返回了意外结果:Google.GData.Client.Utilities.getAuthException(TokenCollection 令牌,HttpWebResponse 响应)在 Google.GData.Client.Utilities.QueryClientLoginToken(GDataCredentials gc,String serviceName)的 405 , 字符串 applicationName, Boolean fUseKeepAlive, IWebProxy proxyServer, Uri clientLoginHandler) 在 Google.GData.Client.GDataGAuthRequest.QueryAuthToken(GDataCredentials gc) 在 Google.GData.Client.GDataGAuthRequest.EnsureCredentials() 在 Google.GData.Client.GDataRequest.EnsureWebRequest( ) 在 Google.GData.Client.GDataGAuthRequest.EnsureWebRequest() 在 Google.GData.Client.GDataGAuthRequest.CopyRequestData() 在 Google.GData.Client.GDataGAuthRequest.Execute(Int32 retryCounter) 在 Google.GData.Client.GDataGAuthRequest。Execute() 在 Google.GData.Client.Service.Insert(Uri feedUri, AtomEntry newEntry, AsyncSendData 数据) 在 Google.GData 的 Google.GData.Client.Service.EntrySend(Uri feedUri, AtomBase baseEntry, GDataRequestType 类型, AsyncSendData 数据) .Client.Service.Insert[TEntry](Uri feedUri, TEntry entry)

4

2 回答 2

3

我今天打了这个,使用与我在网上某处找到的相同的示例代码。

通过一些测试,我发现如果我设置 HTTP 代理(我使用的是 Charles,但其他常见的是 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;

在那之后,它就像一个魅力。

于 2012-11-13T18:30:48.433 回答
0

使用 3.5.12 版再试一次。也许您在代理后面,需要先设置正确的代理设置

于 2012-05-31T08:32:40.163 回答