Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.40/5 (3 votes)
See more:
I getting stuck on the google calendar API v3 documentation.
I DO receive the events created inside the webinterface of google calendar, but as soon as i try to put a new event into the calendar through my .net project I got the error:
Insufficient Permission [403]

Errors [

Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global]

]
Hope somebody could help solving this error! In my opinion the documentation and also the vb.net examples are not giving any solution.


What I have tried:

VB
<pre>Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Calendar.v3
Imports Google.Apis.Calendar.v3.Data
Imports Google.Apis.Calendar.v3.EventsResource
Imports Google.Apis.Services
Imports Google.Apis.Util.Store
Imports System
Imports System.IO
Imports System.Linq
Imports System.Text
Imports System.Threading
Imports System.Threading.Tasks
Imports System.Windows.Forms
Imports System.Collections.Generic
Imports Calendar

Public Class Form1
    Shared Scopes As String() = {CalendarService.Scope.CalendarReadonly}

    Shared ApplicationName As String = "Google Calendar API .NET Quickstart"
    Dim m_appointments As New List(Of Appointment)

   
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim credential As UserCredential
        Using stream = New FileStream("client_secret.json", FileMode.Open, FileAccess.Read)
            Dim credPath As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
            credPath = Path.Combine(credPath, ".credentials/calendar-dotnet-quickstart.json")
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, New FileDataStore(credPath, True)).Result
            Console.WriteLine("Credential file saved to: " & credPath)
        End Using

        Dim service = New CalendarService(New BaseClientService.Initializer() With {.HttpClientInitializer = credential, .ApplicationName = ApplicationName})
        Dim request As EventsResource.ListRequest = service.Events.List("primary")
        request.TimeMin = DateTime.Now
        request.ShowDeleted = False
        request.SingleEvents = True
        request.MaxResults = 10
        request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime
        Dim events As Events = request.Execute()
        If events.Items IsNot Nothing AndAlso events.Items.Count > 0 Then
            For Each [Event] In events.Items
                Dim app As New Appointment
                app.StartDate = [Event].Start.DateTime
                app.EndDate = [Event].End.DateTime
                app.Title = [Event].Summary
                m_appointments.Add(app)
            Next
        Else
        End If
        DayView2.AllowNew = True
        DayView2.StartDate = Now
    End Sub


    Private Sub DayView2_Click(sender As Object, e As EventArgs) Handles DayView2.Click
        'Create NEW event in dayview
        Dim credential As UserCredential
        Using stream = New FileStream("client_secret.json", FileMode.Open, FileAccess.Read)
            Dim credPath As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
            credPath = Path.Combine(credPath, ".credentials/calendar-dotnet-quickstart.json")
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, New FileDataStore(credPath, True)).Result
        End Using
        Dim service = New CalendarService(New BaseClientService.Initializer() With {.HttpClientInitializer = credential, .ApplicationName = ApplicationName})
        Dim CalendarEvent As New Google.Apis.Calendar.v3.Data.Event
        Dim request As EventsResource.InsertRequest
        Dim StartDateTime As New Google.Apis.Calendar.v3.Data.EventDateTime
        Dim enddatetime As New Google.Apis.Calendar.v3.Data.EventDateTime
        Dim A As Date = "16/01/2018 13:00"
        StartDateTime.DateTime = A
        Dim B As Date = "16/01/2018 14:00"
        enddatetime.DateTime = B
        CalendarEvent.Start = StartDateTime
        CalendarEvent.End = enddatetime
        CalendarEvent.Id = System.Guid.NewGuid.ToString
        CalendarEvent.ICalUID = CalendarEvent.Id
        CalendarEvent.Summary = "Test"
        service.Events.Insert(CalendarEvent, "primary").Execute()
    End Sub
End Class
Posted
Updated 23-Aug-18 22:39pm

After a lot of testing I found out that the error is basicly solved by setting the correct Calendar ID for the google calendar. Because google calendar uses either an ID like your gmail address or if you have more calendars synchronized inside of your google calendar the calendar ID is called after the list of different ID's.

The main cause in my code was a Capital use inside the word "primary". It looks like the google calendar ID is Capital sensitive! This solved the half of my code. If you get alarm 404 with everything worked out properly. You should first look after the declaration of the word "primary" without capitals.

Than I looked after the code 403. This was caused by the EVENT_ID from google Calendar.

In several examples everybode tells you that you need to use:
system.GUID.Newguide.tostring


If you look at the google.calendar.API.v3 documentation you will see that the eventID must be an unique ID from 5 up to 32 Characters/numbers. The system.guid.newguide.tostring will give you a ID with 32 chars, but will make sequences of xxxxxxxx-xxxxxxxx-xxxxxxxx-xxxxxxxxx.
The only thing you need to do before the event ID is accepted by Google Calendar without giving you an error is:
System.Guid.NewGuid.ToString.Replace("-", "")


Hope this will help anybody who also wants to work with Google Calendar API in vb.net
 
Share this answer
 
This should help explain the errors: Handle API Errors  |  Calendar API  |  Google Developers[^]

As you can see from the above documentation link, the error information is in the response body.

Also check the inner exception of the WEbException thrown as there is usually more detailed information.
 
Share this answer
 
I made this working with below steps:

Step1
VB
Shared Scopes As String() = {CalendarService.Scope.CalendarReadonly}
Replace above line with
VB
Shared Scopes As String() = {CalendarService.Scope.Calendar}

Step2
VB
CalendarEvent.Id = System.Guid.NewGuid.ToString
CalendarEvent.ICalUID = CalendarEvent.Id
Commented out first line and then changed second line as follows
VB
CalendarEvent.ICalUID = "112233" 'Ofcourse you may try to use the NewGuid here
 
Share this answer
 
Comments
Nelek 24-Aug-18 8:16am    
Did you notice that the question is over 7 months old?
ManikandanChennai 24-Aug-18 22:22pm    
Yes. But no one has answered to complete. Hence I answered. This can help someone in future.
Henkjan 2 28-Sep-18 10:29am    
Hi All. I solved the problem with the API. The problem was with the UID that google automatically give to the item.
Everybody thanks for the help!
Member 13929434 24-Oct-18 19:24pm    
Henkjan, Please, tell us how you solved the problem. What did you do exactly to overcome the problem.
Thanks.
ManikandanChennai 10-May-19 3:31am    
I have clearly explained what I did... follow Step1 and Step2.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900