Why am I getting a (401) unauthorized error in the Google Calendar API

I play with the Google Calendar API and get stuck on something. When I call it below deleting a calendar event , it works great on the first pass and usually the second. However, the second or third time I call this method, I get (401) Unauthorized error . It uses the same credentials every time. If I get an exception, I can reset the credentials in catch, and it works fine. I would rather not do this. Any ideas?

        CalendarService myService = new CalendarService("mycompany-myapp-1");
        myService.setUserCredentials("jo@username.com", "password");


        // set the query for the event
        EventQuery myQuery = new EventQuery(("http://www.google.com/calendar/feeds/jo@username.com/private/full"));
        myQuery.Query = "Cut the grass";
        myQuery.StartTime = DateTime.Now;
        myQuery.EndTime = DateTime.Now.AddDays(1);

        // find the event
        EventFeed myResultsFeed = null;

        try
        {
            // execute the query to find the event
            myResultsFeed = myService.Query(myQuery);                
        }
        catch (Exception ex)
        {
            // this is where i get the unauthorized exception
            // if i reset the credentials here it works fine

            myService.setUserCredentials("jo@username.com", "password"); 
            myResultsFeed = myService.Query(myQuery);
        }

        if (myResultsFeed != null && myResultsFeed.Entries.Count > 0)
        {
            AtomEntry firstMatchEntry = myResultsFeed.Entries[0];
            firstMatchEntry.Delete();
        }
+3
source share
2 answers

, . - fiddler.

0

, -, api . , , 100% .

0

Source: https://habr.com/ru/post/1702196/


All Articles