sevoug
sevoug

Reputation: 169

Google Calendar authentication Error

I am trying to update Google Calendar via latest Google Data API SDK.

My sample code below:

            string sGoogleUserName = "[email protected]";
            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());
        }

here is the error below

Google.GData.Client.GDataRequestException: Execution of authentication request returned unexpected result: 405 at Google.GData.Client.Utilities.getAuthException(TokenCollection tokens, HttpWebResponse response) at 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 type, AsyncSendData data) at Google.GData.Client.Service.Insert(Uri feedUri, AtomEntry newEntry, AsyncSendData data) at Google.GData.Client.Service.Insert[TEntry](Uri feedUri, TEntry entry)

Upvotes: 0

Views: 1913

Answers (2)

user1821705
user1821705

Reputation: 31

I hit this one today, using the same sample code you did that I found somewhere on the net.

With some testing, I figured out that it would work if I set up an HTTP proxy (I was using Charles but other common ones are Fiddler, etc) but not if I attempted without a proxy.

On a hunch I commented out the stuff under //Use Proxy like this and it worked:

string googleUserName = "[email protected]";
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;

After that it worked like a charm.

Upvotes: 3

rt2800
rt2800

Reputation: 3045

try again with version 3.5.12. Maybe you are behind a proxy and need to set the proper Proxy settings first

Upvotes: 0

Related Questions