使用Google calendar API v3创建活动问题 [英] Create event problem with Google calendar API v3

查看:83
本文介绍了使用Google calendar API v3创建活动问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在Google日历API v3文档中。

我会收到在谷歌日历的网络界面中创建的事件,但是一旦我尝试通过日历将新事件放入日历中我的.net项目我得到了错误:

权限不足[403]



错误[



消息[权限不足]位置[ - ]原因[insufficientPermissions]域[全局]



]

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:

<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

推荐答案

经过大量测试后我发现错误是基本的通过为Google日历设置正确的日历ID解决问题。由于Google日历使用的是您的Gmail地址之类的ID,或者如果您在Google日历内部同步了更多日历,则会在不同ID列表之后调用日历ID。



我的代码中的主要原因是在primary一词中使用了Capital。看起来谷歌日历ID是资本敏感的!这解决了我的一半代码。如果你得到报警404,一切正常。您应首先照看没有大写字母的主要一词的声明。



比我照看代码403.这是由谷歌日历的EVENT_ID引起的。



在几个例子中,everybode告诉你需要使用:

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





如果查看google.calendar.API.v3文档,您会看到eventID必须是从5到5的唯一ID 32个字符/数字。 system.guid.newguide.tostring将为您提供一个包含32个字符的ID,但会生成xxxxxxxx-xxxxxxxx-xxxxxxxx-xxxxxxxxx的序列。

在事件ID之前您需要做的唯一事情是谷歌日历接受而不会给你一个错误:



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("-", "")





希望这能帮助任何想要在vb.net中使用Google Calendar API的人



Hope this will help anybody who also wants to work with Google Calendar API in vb.net


这应该有助于解释错误:处理API错误  |  Calendar API  |  Google Developers [ ^ ]



从上面的文档链接可以看出,错误信息在响应正文中。



还检查抛出的WEbException的内部异常,因为通常有更详细的信息。
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.


我使用以下步骤:



Step1

I made this working with below steps:

Step1
Shared Scopes As String() = {CalendarService.Scope.CalendarReadonly}

将以上行替换为

Replace above line with

Shared Scopes As String() = {CalendarService.Scope.Calendar}



Step2


Step2

CalendarEvent.Id = System.Guid.NewGuid.ToString
CalendarEvent.ICalUID = CalendarEvent.Id

注释掉第一行然后按如下方式更改第二行

Commented out first line and then changed second line as follows

CalendarEvent.ICalUID = "112233" 'Ofcourse you may try to use the NewGuid here


这篇关于使用Google calendar API v3创建活动问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆