'Why am I getting a (401) Unauthorized Error in Google Calendar API
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();
}
Solution 1:[1]
I think that more information is needed. The best way is to use fiddler.
Solution 2:[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.
Solution 3:[3]
[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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Igal Serban |
| Solution 2 | JD Roberson |
| Solution 3 |
