Why am I getting a (401) Unauthorized Error in Google Calendar API

梦想与她 提交于 2020-01-15 01:26:09

问题


Ive been playing around with the Google Calendar API and am getting stuck on something. When i call this below to delete a calendar event it works fine on the first pass and usually the second. However, around the 2nd or 3rd time I call this method I get a (401) Unauthorized error. It uses the same credentials every time. If I get the exception, I can reset the credentials in the catch and it works fine. I would prefer not to have to 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();
        }

回答1:


I think that more information is needed. The best way is to use fiddler.




回答2:


You can fix this by creating a webbrowser control that navigates to the api on load and just never show it to the user. Only solution I have found that 100% fixes the issue.



来源:https://stackoverflow.com/questions/476979/why-am-i-getting-a-401-unauthorized-error-in-google-calendar-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!