为什么我在Google Calendar API中收到(401)未经授权的错误 [英] Why am I getting a (401) Unauthorized Error in Google Calendar API

查看:96
本文介绍了为什么我在Google Calendar API中收到(401)未经授权的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Google Calendar API,并且遇到了某些问题.当我在下面将其称为删除日历事件时,它在第一次通过时通常效果很好,通常在第二次通过时效果很好.但是,在第二次或第三次调用此方法时,出现了(401)未经授权的错误.每次使用相同的凭据.如果出现异常,我可以在捕获中重置凭据,并且工作正常.我希望不必这样做.有什么想法吗?

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();
        }

推荐答案

您可以通过创建一个Web浏览器控件来解决此问题,该控件可在加载时导航到api,而永远不会向用户显示.我发现只有解决方法可以100%修复此问题.

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.

这篇关于为什么我在Google Calendar API中收到(401)未经授权的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆