我的.NET系统中有一个模块,用于管理多个用户的Google日历,该日历基于几天前已弃用的V2 API编写。
我的问题是,如何访问和管理多个用户的日历?与 V2 API 一样,我们只需传入用户 ID 和密码即可访问资源。但在 V3 中,使用了 OAuth2。我设法一次为一个用户进行身份验证。在此之后,我如何访问其他用户日历?(例如:logut 用户 1 并再次使用用户 2 登录?到目前为止,我找不到任何相关示例。
Sub Main()
Dim secret As New ClientSecrets
secret.ClientId = "MyClientID.apps.googleusercontent.com"
secret.ClientSecret = "MyClientSecret"
Dim init As New Flows.AuthorizationCodeFlow.Initializer("https://accounts.google.com/o/oauth2/auth", "https://accounts.google.com/o/oauth2/token")
init.ClientSecrets = secret
Dim flow As New Flows.AuthorizationCodeFlow(init)
Dim token As Responses.TokenResponse = flow.LoadTokenAsync("user1@gmail.com", CancellationToken.None).Result
Dim credential As New UserCredential(flow, "user1@gmail.com", token)
Try
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(secret, {CalendarService.Scope.Calendar}, "user", CancellationToken.None, New FileDataStore("CAS.GoogleConnector")).Result
Catch ex As Exception
MsgBox(ex.Message)
End Try
' Create the calendar service using an initializer instance
Dim initializer As New BaseClientService.Initializer()
initializer.HttpClientInitializer = credential
initializer.ApplicationName = "Clinic Assist"
Dim service As CalendarService
service = New CalendarService(initializer)
Dim a As CalendarsResource.GetRequest = service.Calendars.Get("user1@gmail.com")
a.Execute()
Console.WriteLine("Press any key to continue...")
Dim key As ConsoleKeyInfo = Console.ReadKey
If key.Key = ConsoleKey.N Then
NewEvent(service)
ElseIf key.Key = ConsoleKey.D Then
DeleteEvent(service)
ElseIf key.Key = ConsoleKey.C Then
NewCalendar(service)
End If
End Sub
Private Sub NewEvent(service As CalendarService)
Dim startDateTime, endDateTime As New Data.EventDateTime
startDateTime.DateTime = "2014-12-01T16:00:00"
endDateTime.DateTime = "2014-12-01T17:00:00"
Dim eventData As New Data.Event
With eventData
.Start = startDateTime
.End = endDateTime
.Location = "ALocation"
.Summary = "TRY"
End With
Try
Dim insertRequest As InsertRequest = service.Events.Insert(eventData, "user1@gmail.com")
insertRequest.Execute()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
我尝试将用户 ID 从"user1@gmail.com"更改为"user2@gmail.com",它似乎也访问了 user1 日历。如果有人能帮忙,不胜感激。
如果所有用户都在单个域中,则可以使用服务帐户。服务帐户属于您的应用程序,而不是单个最终用户。您的应用代表服务帐号调用 Google API,因此用户不会直接参与其中。
如果用户不在同一域中,则需要在应用程序中存储每个用户的凭据。这是对您有所帮助的链接。