为什么我在 Google Calendar API 中收到 (401) 未经授权的错误
我一直在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
[NODE JS] 我知道那是 8 年前的事了,但不知何故我遇到了这样的错误并从堆栈中得到了这个:)我忘记将“auth”对象传递到请求中。 完整的架构是:
也许在你的情况下问题是刷新/访问令牌......我不确定。
[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:
Maybe in your case the problem is with refresh/access token... I am not sure.
我认为需要更多信息。 最好的方法是使用 fiddler。
I think that more information is needed. The best way is to use fiddler.
您可以通过创建一个 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.