Reputation: 21
I have a Google account created in 2020. I can crate an event in it's Google Calendar by API and C# code.
OAuth2 authentication is used to create events.Credentials and Token JSON
is created.
I have created new Google Account in 2021. Using the same code, the Google event is not created. Credetials
are same and new token
is created.
but the code Gives error.
The service calendar has thrown an exception: Google.GoogleApiException: Google.Apis.Requests.RequestError
Invalid Value [400]
Errors [
Message[Invalid Value] Location[ - ] Reason[invalid] Domain[global]
]
I could not find solution in this forum. So I am putting up the question here. Also, Google Calendar Cog-Wheel of 2020 Account shows, Trash for the deleted events. For Google Calendar Cog-Wheel of 2021 account shows Bin for the deleted events. Is there any core difference in both accounts? Is that reason I am getting [400] error? Google Account created in 2020
Google Account Created in 2021
using Google.Apis.Auth.OAuth2;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Data;
using System.IO;
using System.Threading;
CODE Initialisation
private string _TokenName;
private string _start;
private string _end;
private string _until;
private string _summary;
private string _location;
private string _calendarID;
private int _ColorID; // (11 OR 6)
private string _status; // CONFIRMED (Booking)/ Tentative (Enquiry)
private string _eventID;
static string[] Scopes = { CalendarService.Scope.Calendar,CalendarService.Scope.CalendarEvents };
static string ApplicationName = "Marking in Google Calendar";
private int _setid;
Define Token Name, Google Calendar Id
public int SetId
{
get { return _setid; }
set
{
_setid = value;
// get values from DB
_calendarID = <[email protected]>;
_TokenName = <token_setid.json>;
_ColorID = <calendarColor>;
}
}
Form - TextBox - SetID, DatePicker - dtpCalendar, DatePicker - dtpTime, TextBox - txtEventID, CommandButton - btnApply
private void btnApply_Click(object sender, EventArgs e)
{
DateTime tdt;
int tmShift;
SetId = Convert.ToInt32(txtSetId.Text);
tdt = dtpCalendar.Value.Date;
tmShift = dtpTime.Value.Hour;
try
{
_start = tdt.ToString("yyyy-MM-dd") + "T" + (startTime).ToString("HH:mm:00") + "+0530";
_end = tdt.ToString("yyyy-MM-dd") + "T" + (endTime).ToString("HH:mm:00") + "+0530";
_until = "RRULE:FREQ=DAILY;UNTIL=" + tDt.ToString("yyyyMMdd") + "T100000Z";
_summary = "IGNORE: TEST " ;
_location = "test location";
_eventID = txtEventID.Text.ToString() + DateTime.Now.ToString("yyyyMMddHHmmss");
EventCreate();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public bool EventCreate()
{
try
{
bool result;
result = false;
CalendarService calendarService = GetCalendarService(_TokenName);
result = EventMaking(calendarService);
return result;
}
catch (Exception ex)
{
if (ex.Message.Contains("(409)")) { Console.WriteLine("Returned from try-catch-409"); return true; }
else throw ex;
}
}
private CalendarService GetCalendarService(string TokenName)
{
try
{
UserCredential credential;
using (var stream =
new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = TokenName;
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
<[email protected]>, //here will have to pass the actual user
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
// Create Google Calendar API service.
CalendarService service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
return service;
}
catch (Exception ex)
{
throw;
}
}
private bool EventMaking(CalendarService _service)
{
try
{
TimeZoneInfo INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
Event body = new Event()
{
Summary = _summary,
Description = _location,
Start = new EventDateTime()
{
DateTime = Convert.ToDateTime(_start),
TimeZone = INDIAN_ZONE.DisplayName
},
End = new EventDateTime()
{
DateTime = Convert.ToDateTime(_end),
TimeZone = INDIAN_ZONE.DisplayName
},
Recurrence = new String[] {
_until
},
ColorId = _ColorID.ToString()
};
body.Id = _eventID.ToLower(); // ID as 0-9 and a-v characters
_service.Events.Insert(body, _calendarID).Execute(); // THROWS ERROR HERE
return true;
}
catch (Exception ex)
{
throw ex;
}
}
}
Upvotes: 1
Views: 653
Reputation: 21
Finally, I resolved the error. This is code of defining BODY of the EVENT.
TimeZoneInfo INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
Event body = new Event()
{
Summary = _summary,
Description = _location,
Start = new EventDateTime()
{
DateTime = Convert.ToDateTime(_start),
TimeZone = INDIAN_ZONE.DisplayName
},
End = new EventDateTime()
{
DateTime = Convert.ToDateTime(_end),
TimeZone = INDIAN_ZONE.DisplayName
},
Recurrence = new String[] {
_until
},
ColorId = _ColorID.ToString() // CODE allowed range is '1' to '11'
};
body.Id = _eventID.ToLower(); // ID as 0-9 and a-v characters
Value of _ColorID was greater than 11. The Error in Post was thrown because of this invalid value of ColorId. Google Calendar allows only 11 colors for an Event in Calendar. Check the allowed colors on this link
Upvotes: 1