我正在尝试实施Google日历,但无法添加新事件。我必须更改GoogleAPI控制台中的一些秘密设置吗?
无论如何,这是我的代码:
线程({ val p12 = context.getAssets()。open(“ p12.p12”)
val credentials = GoogleCredential.Builder().setTransport(NetHttpTransport())
.setJsonFactory(JsonFactory)
.setServiceAccountId("acountID")
.setServiceAccountScopes(Arrays.asList(CalendarScopes.CALENDAR))
.setServiceAccountPrivateKeyFromP12File(stream2file(p12))
.build()
println(credentials.getServiceAccountId()) // this only works if you have a service account
val service = Calendar.Builder(NetHttpTransport(), JsonFactory, credentials)
.setHttpRequestInitializer(credentials)
.setApplicationName("APPNAME")
.build()
var event = Event()
.setSummary("Google I/O 2015")
.setLocation("800 Howard St., San Francisco, CA 94103")
.setDescription("A chance to hear more about Google's developer products.")
val startDateTime = DateTime("2015-05-28T09:00:00-07:00")
val start = EventDateTime()
.setDateTime(startDateTime)
.setTimeZone("America/Los_Angeles")
event.start = start
val endDateTime = DateTime("2015-05-28T17:00:00-07:00")
val end = EventDateTime()
.setDateTime(endDateTime)
.setTimeZone("America/Los_Angeles")
event.end = end
val recurrence = "RRULE:FREQ=DAILY;COUNT=2"
event.setRecurrence(Arrays.asList(recurrence))
val attendees = arrayOf(EventAttendee().setEmail("lpage@example.com"), EventAttendee().setEmail("sbrin@example.com"))
event.setAttendees(attendees.toMutableList())
val reminderOverrides = arrayOf(EventReminder().setMethod("email").setMinutes(24 * 60), EventReminder().setMethod("popup").setMinutes(10))
val reminders = Event.Reminders()
.setUseDefault(false)
.setOverrides(reminderOverrides.toMutableList())
event.reminders = reminders
val calendarId = "primary"
event.anyoneCanAddSelf = true
Log.e("calList","${service.CalendarList().list()}")
service.events().insert(calendarId, event).execute()
System.out.printf("Event created: %s\n", event.toList())
System.out.printf("Event created: %s\n", event.htmlLink)
})。start()
运行此代码后,我返回一个链接,该链接创建了我的事件。但是,当我输入链接时,找不到该请求。就像我没有正确的权限。