我设法通过OAuth2.0进行身份验证并从serviceaccount创建日历服务,然后添加一些日历(比如2661j6vlkriufg0ug8e2dtlbps@group.calendar.google.com),存储他们的ID。但是当我尝试向这些日历添加事件时,即使没有错误并且“events.insert.execute”按预期返回事件,只要我尝试从这些日历中读取插入的事件,就不会返回任何事件,我也无法看到或添加这些日历到我的主日历。我怎样才能实现这些目标?我怎样才能看到群组名称是什么?
以下是代码:
Private Const SVCACCMAIL = "nnnnnnnnnnnnn-xxxxxxxxxxxxxxxmmmmmmmmmm@developer.gserviceaccount.com"
Private Const ACTIVITY_ID = "x9x9x9x9x9x9x9x9x9x9x9x9x9x9"
Public Function GetCert() As X509Certificate2
Dim fileCert As String
If System.IO.File.Exists("D:\inetpub\wwwroot\UGO_Cont.Web\UGO-Adv-CRM-nnnnnnnnaaaaaaa.p12") Then
fileCert = "D:\inetpub\wwwroot\UGO_Cont.Web\UGO-Adv-CRM-nnnnnnnnnnna.p12"
Else
fileCert = "C:\inetpub\wwwroot\UGO_Cont.Web\UGO-Adv-CRM-nnnnnnnnnnnna.p12"
End If
Dim cert = New X509Certificate2(fileCert, "notasecret", X509KeyStorageFlags.Exportable)
Return cert
End Function
<OperationContract()>
Public Function InsertEvent(authToken As String, id_Azienda As Integer, id_Azione As Integer, id_Op As Integer) As String
Dim lscop = New List(Of String)
lscop.Add(CalendarService.Scope.Calendar)
Dim scopes As IEnumerable(Of String) = lscop
'Dim inzlzr As New ServiceAccountCredential.Initializer(SVCACCMAIL) With {.User = "xxxxxxxxxx@gmail.com", .Scopes = scopes}
'Dim inzlzr As New ServiceAccountCredential.Initializer(SVCACCMAIL) With {.User = SVCACCMAIL, .Scopes = scopes}
Dim inzlzr As New ServiceAccountCredential.Initializer(SVCACCMAIL) With {.Scopes = scopes}
Dim credential As New ServiceAccountCredential(inzlzr.FromCertificate(GetCert))
Dim ptx As New Pratiche_Entities
Dim azsCrm = From a In ptx.Azioni Where a.Id_Azienda = id_Azienda AndAlso a.Id_Azione = id_Azione
If azsCrm IsNot Nothing And azsCrm.Count = 1 Then
Dim taskOp = InsertEventAsync(credential, azsCrm.First)
End If
End Function
Async Function InsertEventAsync(cr As ServiceAccountCredential, azCrm As Azioni) As System.Threading.Tasks.Task(Of String)
Dim otx As New OperAziende_Entities
Dim ops = From o In otx.Operatori Where o.Id_Azienda = azCrm.Id_Azienda AndAlso o.Id_Operatore = azCrm.id_OperatoreAssegnato
If ops IsNot Nothing AndAlso ops.Count = 1 AndAlso ops.First.Mail_Operatore IsNot Nothing Then
Dim op = ops.First
Dim GetToken = cr.RequestAccessTokenAsync(System.Threading.CancellationToken.None)
Dim ok As Boolean = Await GetToken
Dim service As New CalendarService(New BaseClientService.Initializer() With {.ApplicationName = "UGO_Adv CRM", .HttpClientInitializer = cr})
Dim Id = Nothing
'list calendars
Dim list As IList(Of CalendarListEntry) = service.CalendarList.List().Execute().Items()
Dim flgupd As Boolean = False
For Each l In list
'Dim url=l.
If l.Summary = Op.Mail_Operatore Then
Id = l.Id
If Op.CalendarId Is Nothing OrElse Op.CalendarId <> Id Then
Op.CalendarId = Id
flgupd = True
Dim evts = service.Events.List(Id).Execute
Dim evs = evts
End If
End If
Next
If Id Is Nothing Then
'insert new calendar
Dim newCal = service.Calendars.Insert(New Calendar() With {.Summary = Op.Mail_Operatore}).Execute
Id = newCal.Id
Op.CalendarId = Id
flgupd = True
End If
If flgupd Then
otx.SaveChanges()
End If
'insert a new event
Dim lAtts As New List(Of EventAttendee)
Dim att As New EventAttendee() With {.Email = op.Mail_Operatore, .DisplayName = op.Dsc_Operatore}
lAtts.Add(att)
Dim atts As IList(Of EventAttendee) = lAtts
'Dim lrmnd As New List(Of EventReminder)
'lrmnd.Add(New EventReminder() With {.Method = "email", .Minutes = 30})
'Dim rmnds As IList(Of EventReminder) = lrmnd
Try
Dim newEvt As New [Event]()
With newEvt
.Summary = azCrm.Cod_VociTipologiaAzioni + " x " + CStr(azCrm.Id_Anagrafica)
.Attendees = atts
'.Reminders = rmnds
.Description = azCrm.Dsc_Azione
.Start = New EventDateTime() With {.DateTime = Format(azCrm.data_attesa_esecuzione, "yyyy-MM-ddTHH:mm:00").Replace(".", ":"), .TimeZone = "Europe/Rome"}
.End = New EventDateTime() With {.DateTime = Format(If(azCrm.fine_attesa_esecuzione, azCrm.data_attesa_esecuzione), "yyyy-MM-ddTHH:mm:00").Replace(".", ":"), .TimeZone = "Europe/Rome"}
End With
Dim evReq = service.Events.Insert(newEvt, Id)
evReq.SendNotifications = True
Dim ev = evReq.Execute
Dim ev1 = ev
Catch ex As Google.GoogleApiException
Dim err = ex.Message
End Try
End If
End Function
提前致谢。