为什么我在 Google Calendar API 中收到 (401) 未经授权的错误

发布于 2024-07-12 07:02:23 字数 1782 浏览 7 评论 0原文

我一直在使用 Google Calendar API,但遇到了一些问题。 当我在下面调用它来删除日历事件时,它在第一遍(通常是第二遍)上运行良好。 但是,大约在第二次或第三次调用此方法时,我收到(401)未经授权的错误。 它每次都使用相同的凭据。 如果出现异常,我可以在 catch 中重置凭据并且工作正常。 我宁愿不必这样做。 有任何想法吗?

        CalendarService myService = new CalendarService("mycompany-myapp-1");
        myService.setUserCredentials("[email protected]", "password");


        // set the query for the event
        EventQuery myQuery = new EventQuery(("http://www.google.com/calendar/feeds/[email protected]/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("[email protected]", "password"); 
            myResultsFeed = myService.Query(myQuery);
        }

        if (myResultsFeed != null && myResultsFeed.Entries.Count > 0)
        {
            AtomEntry firstMatchEntry = myResultsFeed.Entries[0];
            firstMatchEntry.Delete();
        }

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("[email protected]", "password");


        // set the query for the event
        EventQuery myQuery = new EventQuery(("http://www.google.com/calendar/feeds/[email protected]/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("[email protected]", "password"); 
            myResultsFeed = myService.Query(myQuery);
        }

        if (myResultsFeed != null && myResultsFeed.Entries.Count > 0)
        {
            AtomEntry firstMatchEntry = myResultsFeed.Entries[0];
            firstMatchEntry.Delete();
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

国粹 2024-07-19 07:02:23

[NODE JS] 我知道那是 8 年前的事了,但不知何故我遇到了这样的错误并从堆栈中得到了这个:)我忘记将“auth”对象传递到请求中。 完整的架构是:

    interface Params$Resource$Events$Delete extends StandardParameters {
        /**
         * Auth client or API Key for the request
         */
        auth?: string | OAuth2Client | JWT | Compute | UserRefreshClient;
        /**
         * Calendar identifier. To retrieve calendar IDs call the calendarList.list
         * method. If you want to access the primary calendar of the currently
         * logged in user, use the "primary" keyword.
         */
        calendarId?: string;
        /**
         * Event identifier.
         */
        eventId?: string;
        /**
         * Deprecated. Please use sendUpdates instead.  Whether to send
         * notifications about the deletion of the event. Note that some emails
         * might still be sent even if you set the value to false. The default is
         * false.
         */
        sendNotifications?: boolean;
        /**
         * Guests who should receive notifications about the deletion of the event.
         */
        sendUpdates?: string;
    }

也许在你的情况下问题是刷新/访问令牌......我不确定。

[NODE JS] I know it was 8 years ago but somehow I got into such an error and got this from stack :) I forgot to pass 'auth' object into request. The full schema is:

    interface Params$Resource$Events$Delete extends StandardParameters {
        /**
         * Auth client or API Key for the request
         */
        auth?: string | OAuth2Client | JWT | Compute | UserRefreshClient;
        /**
         * Calendar identifier. To retrieve calendar IDs call the calendarList.list
         * method. If you want to access the primary calendar of the currently
         * logged in user, use the "primary" keyword.
         */
        calendarId?: string;
        /**
         * Event identifier.
         */
        eventId?: string;
        /**
         * Deprecated. Please use sendUpdates instead.  Whether to send
         * notifications about the deletion of the event. Note that some emails
         * might still be sent even if you set the value to false. The default is
         * false.
         */
        sendNotifications?: boolean;
        /**
         * Guests who should receive notifications about the deletion of the event.
         */
        sendUpdates?: string;
    }

Maybe in your case the problem is with refresh/access token... I am not sure.

梦里°也失望 2024-07-19 07:02:23

我认为需要更多信息。 最好的方法是使用 fiddler

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

邮友 2024-07-19 07:02:23

您可以通过创建一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文