我正在尝试使用kotlin和V3API为google日历设置推送通知。
这是我的类,包含用于crud操作的函数和事件:
import com.google.api.client.auth.oauth2.Credential
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport
import com.google.api.client.http.javanet.NetHttpTransport
import com.google.api.client.json.JsonFactory
import com.google.api.client.json.jackson2.JacksonFactory
import com.google.api.client.util.DateTime
import com.google.api.client.util.store.FileDataStoreFactory
import com.google.api.services.calendar.Calendar
import com.google.api.services.calendar.CalendarScopes
import com.google.api.services.calendar.model.*
import java.io.*
class CalendarAPI {
private val appName = "Calendar"
private val jsonFactory: JsonFactory = JacksonFactory.getDefaultInstance()
/**
* Global instance of the scopes required by this quickstart.
* If modifying these scopes, delete your previously saved tokens/ folder.
*/
private val scopes = listOf(CalendarScopes.CALENDAR)
private val credentialsPath = "/Credentials.json"
// Build a new authorized API client service.
private val httpTransport = GoogleNetHttpTransport.newTrustedTransport()
val service: Calendar =
Calendar.Builder(httpTransport, jsonFactory, getCredentials(httpTransport))
.setApplicationName(appName)
.build()
// List the next 10 events from the primary calendar.
fun getNext10Events(now: DateTime): Events {
return service.events().list("primary")
.setMaxResults(10)
.setTimeMin(now)
.setOrderBy("startTime")
.setSingleEvents(true)
.execute()
}
fun printEvents(events: Events) {
val items = events.items
if (items.isEmpty()) {
println("No events found!")
} else {
for (event in items) {
val start = if (event.start.dateTime == null)
event.start.date else event.start.dateTime
val end = if (event.end.dateTime == null)
event.end.date else event.end.dateTime
println("${event.summary} starts:($start), ends:($end)")
}
}
}
fun deleteEvents(calendarId: String, events: Events, range: List<Int>) {
for (i in range) {
try {
service.events().delete(calendarId, events.items[i].id).execute()
} catch (e: IndexOutOfBoundsException) {
throw Exception("Deletion of events failed! Probably index out of bounds.")
}
}
}
fun deleteAllEvents(calendarId: String) {
service.calendars().clear(calendarId).execute()
}
fun createEvent(
calendarId: String,
startDateTime: DateTime,
endDateTime: DateTime,
summary: String? = null,
location: String? = null,
description: String? = null,
recurrence: List<String> = emptyList(),
attendees: List<EventAttendee> = emptyList(),
reminder: List<EventReminder> = emptyList(),
timeZone: String = "(GMT+03:00) Eastern European Time - Sofia",
sendEmailNotification: Boolean = false
): Event {
val event: Event = Event()
.setSummary(summary)
.setLocation(location)
.setDescription(description)
event.recurrence = recurrence
event.attendees = attendees
val reminders: Event.Reminders = Event.Reminders()
.setUseDefault(false)
.setOverrides(reminder)
event.reminders = reminders
val start = EventDateTime()
.setDateTime(startDateTime)
.setTimeZone(timeZone)
event.start = start
val end = EventDateTime()
.setDateTime(endDateTime)
.setTimeZone("America/Los_Angeles")
event.end = end
service.events().insert(calendarId, event).setSendNotifications(sendEmailNotification).execute()
return event
}
fun updateEvent(
event: Event,
startDateTime: DateTime? = null,
endDateTime: DateTime? = null,
summary: String? = null,
location: String? = null,
description: String? = null,
recurrence: List<String> = emptyList(),
attendees: List<EventAttendee> = emptyList(),
reminder: List<EventReminder> = emptyList(),
timeZone: String = "(GMT+03:00) Eastern European Time - Sofia",
sendEmailNotification: Boolean = false
) {
if (startDateTime != null) {
event.start = EventDateTime().setDateTime(startDateTime).setTimeZone(timeZone)
}
if (endDateTime != null) {
event.end = EventDateTime().setDateTime(endDateTime).setTimeZone(timeZone)
}
event.summary = summary
event.location = location
event.description = description
event.recurrence = recurrence
event.attendees = attendees
val reminders: Event.Reminders = Event.Reminders()
.setUseDefault(false)
.setOverrides(reminder)
event.reminders = reminders
service.events().update("primary", event.id, event).setSendNotifications(sendEmailNotification).execute()
}
fun addAttendeesToEvent(event: Event, attendees: List<EventAttendee>, sendEmailNotification: Boolean = false){
event.attendees.addAll(attendees)
service.events().update("primary", event.id, event).setSendNotifications(sendEmailNotification).execute()
}
/**
* Creates an authorized Credential object.
* @param HTTP_TRANSPORT The network HTTP Transport.
* @return An authorized Credential object.
* @throws IOException If the credentials.json file cannot be found.
*/
private fun getCredentials(HTTP_TRANSPORT: NetHttpTransport): Credential {
// Load client secrets.
val inputStream: InputStream =
CalendarAPI::class.java.getResourceAsStream(credentialsPath)
?: throw FileNotFoundException("Resource not found: $credentialsPath")
val clientSecrets = GoogleClientSecrets.load(
jsonFactory,
InputStreamReader(inputStream)
)
// Build flow and trigger user authorization request.
val flow = GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT,
jsonFactory,
clientSecrets,
scopes
)
.setDataStoreFactory(FileDataStoreFactory(File("token")))
.setAccessType("offline")
.build()
val receiver = LocalServerReceiver.Builder().setPort(8888).build()
return AuthorizationCodeInstalledApp(flow, receiver).authorize("user")
}
}
这是我的主要文件:
package com.example.appolicacalendar
import com.google.api.client.util.DateTime
import com.google.api.services.calendar.model.Channel
fun main() {
val calendar = CalendarAPI()
val channel = Channel()
// channel.address = "forChannel"
channel.token = "forChannel"
val param = calendar.service.events().watch("primary", channel)
println("Upcoming events:")
val events = calendar
.getNext10Events(DateTime(System.currentTimeMillis()))
calendar.printEvents(events)
// calendar.deleteEvents("primary", events, arrayListOf(0))
calendar.deleteAllEvents("primary")
println("\nNew events:")
val event = calendar.createEvent(
summary = "CalendarEvent",
calendarId = "primary",
startDateTime = DateTime("2021-07-08T14:00:00.000+03:00"),
endDateTime = DateTime("2021-07-08T15:00:00.000+03:00")
)
// calendar.updateEvent(event, summary = "CalendarEvent")
calendar.printEvents(calendar.getNext10Events(DateTime(System.currentTimeMillis())))
}
我看到watch方法的一个参数是com.google.api.services.calendar.model.channel,正如您所看到的,我已经创建了它,但是idk如何使用它。
我的目标是记录并查看插入/更新/删除操作何时发生更改。
暂无答案!
目前还没有任何答案,快来回答吧!