无法使用 Google 日历 Api 封锁房间
Unable to block room using Google calendar Api
这是我的 java 代码,我正在尝试使用此代码创建活动(房间是使用资源 Google 日历 API 添加的)活动在房间 A 中创建成功. 但是,当我登记 Google 日历并尝试查看其中的可用房间时,A 房间可用。我希望它不应该显示或者它应该显示罢工任何人都可以告诉我解决方案我在哪里做我错了是否存在权限问题请建议我。
public class CalendarQuickstart {
private static final String APPLICATION_NAME = "API Quickstart";
private static final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"),
".credentials/calendar-java-quickstart");
private static FileDataStoreFactory DATA_STORE_FACTORY;
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static HttpTransport HTTP_TRANSPORT;
private static final List < String > SCOPES = Arrays.asList(CalendarScopes.CALENDAR);
static {
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
}
}
public static Credential authorize() throws IOException {
// Load client secrets.
/*InputStream in = CalendarQuickstart.class.getResourceAsStream("/client_secret.json");
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
clientSecrets, SCOPES).setDataStoreFactory(DATA_STORE_FACTORY).setAccessType("offline").build();
Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;*/
Credential credential = GoogleCredential.fromStream(CalendarQuickstart.class.getResourceAsStream("/client_secret.json"))
.createScoped(SCOPES);
return credential;
}
public static com.google.api.services.calendar.Calendar getCalendarService() throws IOException {
Credential credential = authorize();
return new com.google.api.services.calendar.Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME).build();
}
public static void createEvent() throws IOException {
Event event = new Event().setSummary("Google I/O 2015")
.setDescription("A chance to hear more about Google's developer products.");
DateTime startDateTime = new DateTime("2017-02-27T22:00:00+05:30");
EventDateTime start = new EventDateTime().setDateTime(startDateTime).setTimeZone("Asia/Kolkata");
event.setStart(start);
DateTime endDateTime = new DateTime("2017-02-27T23:00:00+05:30");
EventDateTime end = new EventDateTime().setDateTime(endDateTime).setTimeZone("Asia/Kolkata");
event.setEnd(end);
EventAttendee[] attendees = new EventAttendee[] {
new EventAttendee().setEmail("account@gmail.com"),
new EventAttendee().setEmail("anil@gmail.com"), new EventAttendee().
setEmail("company.com_35353134363037362d333130@resource.calendar.google.com").setResponseStatus("accepted")
};
event.setAttendees(Arrays.asList(attendees));
EventReminder[] reminderOverrides = new EventReminder[] {
new EventReminder().setMethod("email").setMinutes(24 * 60),
new EventReminder().setMethod("popup").setMinutes(10),
};
Event.Reminders reminders = new Event.Reminders().setUseDefault(false)
.setOverrides(Arrays.asList(reminderOverrides));
event.setReminders(reminders);
String calendarId = "primary";
event = getCalendarService().events().insert(calendarId, event).execute();
System.out.printf("Event created: %s\n", event.getId());
}
public static void updateEvent() throws IOException {
Event event = getCalendarService().events().get("primary", "3k90eohao76bk3vlgs8k5is6h0").execute();
event.setSummary("Appointment at Somewhere");
// Update the event
Event updatedEvent = getCalendarService().events().update("primary", event.getId(), event).execute();
System.out.println(updatedEvent.getUpdated());
}
public static void main(String[] args) throws IOException {
com.google.api.services.calendar.Calendar service = getCalendarService();
DateTime now = new DateTime(System.currentTimeMillis());
Events events = service.events().list("primary").setMaxResults(10).setTimeMin(now).setOrderBy("startTime")
.setSingleEvents(true).execute();
List < Event > items = events.getItems();
if (items.size() == 0) {
System.out.println("No upcoming events found.");
} else {
System.out.println("\nUpcoming events");
for (Event event: items) {
DateTime start = event.getStart().getDateTime();
if (start == null) {
start = event.getStart().getDate();
}
System.out.printf("%s (%s)\n", event.getSummary(), start);
}
}
createEvent();
}
您正在使用服务帐户。您需要记住的是服务帐户不是您。服务帐户有自己的 Google 日历帐户 Primary 是其主日历。
String calendarId = "primary";
event = getCalendarService().events().insert(calendarId, event).execute();
这将向服务帐户主要 Google 日历添加一个事件,您无法在 Web 上直观地看到该事件。
您是否尝试过从您的代码中执行 events.list 这应该会向您显示服务帐户 google 日历上的事件。
如果您想直观地看到它,我建议您在自己的个人 Google 日历帐户上创建一个日历,并通过与服务帐户电子邮件地址共享来授予您的服务帐户访问权限。
我的博客 post 关于 service accounts
大家好,经过 google 的长期搜索,我找到了解决方案。
创建事件的步骤 google 事件。
第 1 步:设置以下范围以授权 api。
第二步:授权时要求获得管理和查看日历的权限,使用时必须允许。
并将生成授权码。
第三步:通过生成的授权码
创建access_token
第 4 步:将生成的 access_token 传递给 craete google 事件。
Java 创建 google 事件的代码
public static com.google.api.services.calendar.Calendar getCalendarService() {
GoogleCredential credential = new GoogleCredential().setAccessToken(access_token);
return new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).build();
}
这些步骤在使用 Google 日历 api.
创建活动时为我工作
我已经尝试过使用服务帐户的另一种方式,在这种情况下我们可以创建活动但无法阻止房间。
这是我的 java 代码,我正在尝试使用此代码创建活动(房间是使用资源 Google 日历 API 添加的)活动在房间 A 中创建成功. 但是,当我登记 Google 日历并尝试查看其中的可用房间时,A 房间可用。我希望它不应该显示或者它应该显示罢工任何人都可以告诉我解决方案我在哪里做我错了是否存在权限问题请建议我。
public class CalendarQuickstart {
private static final String APPLICATION_NAME = "API Quickstart";
private static final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"),
".credentials/calendar-java-quickstart");
private static FileDataStoreFactory DATA_STORE_FACTORY;
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static HttpTransport HTTP_TRANSPORT;
private static final List < String > SCOPES = Arrays.asList(CalendarScopes.CALENDAR);
static {
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
}
}
public static Credential authorize() throws IOException {
// Load client secrets.
/*InputStream in = CalendarQuickstart.class.getResourceAsStream("/client_secret.json");
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
clientSecrets, SCOPES).setDataStoreFactory(DATA_STORE_FACTORY).setAccessType("offline").build();
Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;*/
Credential credential = GoogleCredential.fromStream(CalendarQuickstart.class.getResourceAsStream("/client_secret.json"))
.createScoped(SCOPES);
return credential;
}
public static com.google.api.services.calendar.Calendar getCalendarService() throws IOException {
Credential credential = authorize();
return new com.google.api.services.calendar.Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME).build();
}
public static void createEvent() throws IOException {
Event event = new Event().setSummary("Google I/O 2015")
.setDescription("A chance to hear more about Google's developer products.");
DateTime startDateTime = new DateTime("2017-02-27T22:00:00+05:30");
EventDateTime start = new EventDateTime().setDateTime(startDateTime).setTimeZone("Asia/Kolkata");
event.setStart(start);
DateTime endDateTime = new DateTime("2017-02-27T23:00:00+05:30");
EventDateTime end = new EventDateTime().setDateTime(endDateTime).setTimeZone("Asia/Kolkata");
event.setEnd(end);
EventAttendee[] attendees = new EventAttendee[] {
new EventAttendee().setEmail("account@gmail.com"),
new EventAttendee().setEmail("anil@gmail.com"), new EventAttendee().
setEmail("company.com_35353134363037362d333130@resource.calendar.google.com").setResponseStatus("accepted")
};
event.setAttendees(Arrays.asList(attendees));
EventReminder[] reminderOverrides = new EventReminder[] {
new EventReminder().setMethod("email").setMinutes(24 * 60),
new EventReminder().setMethod("popup").setMinutes(10),
};
Event.Reminders reminders = new Event.Reminders().setUseDefault(false)
.setOverrides(Arrays.asList(reminderOverrides));
event.setReminders(reminders);
String calendarId = "primary";
event = getCalendarService().events().insert(calendarId, event).execute();
System.out.printf("Event created: %s\n", event.getId());
}
public static void updateEvent() throws IOException {
Event event = getCalendarService().events().get("primary", "3k90eohao76bk3vlgs8k5is6h0").execute();
event.setSummary("Appointment at Somewhere");
// Update the event
Event updatedEvent = getCalendarService().events().update("primary", event.getId(), event).execute();
System.out.println(updatedEvent.getUpdated());
}
public static void main(String[] args) throws IOException {
com.google.api.services.calendar.Calendar service = getCalendarService();
DateTime now = new DateTime(System.currentTimeMillis());
Events events = service.events().list("primary").setMaxResults(10).setTimeMin(now).setOrderBy("startTime")
.setSingleEvents(true).execute();
List < Event > items = events.getItems();
if (items.size() == 0) {
System.out.println("No upcoming events found.");
} else {
System.out.println("\nUpcoming events");
for (Event event: items) {
DateTime start = event.getStart().getDateTime();
if (start == null) {
start = event.getStart().getDate();
}
System.out.printf("%s (%s)\n", event.getSummary(), start);
}
}
createEvent();
}
您正在使用服务帐户。您需要记住的是服务帐户不是您。服务帐户有自己的 Google 日历帐户 Primary 是其主日历。
String calendarId = "primary";
event = getCalendarService().events().insert(calendarId, event).execute();
这将向服务帐户主要 Google 日历添加一个事件,您无法在 Web 上直观地看到该事件。
您是否尝试过从您的代码中执行 events.list 这应该会向您显示服务帐户 google 日历上的事件。
如果您想直观地看到它,我建议您在自己的个人 Google 日历帐户上创建一个日历,并通过与服务帐户电子邮件地址共享来授予您的服务帐户访问权限。
我的博客 post 关于 service accounts
大家好,经过 google 的长期搜索,我找到了解决方案。
创建事件的步骤 google 事件。
第 1 步:设置以下范围以授权 api。
第二步:授权时要求获得管理和查看日历的权限,使用时必须允许。 并将生成授权码。
第三步:通过生成的授权码
创建access_token第 4 步:将生成的 access_token 传递给 craete google 事件。
Java 创建 google 事件的代码
public static com.google.api.services.calendar.Calendar getCalendarService() {
GoogleCredential credential = new GoogleCredential().setAccessToken(access_token);
return new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).build();
}
这些步骤在使用 Google 日历 api.
创建活动时为我工作我已经尝试过使用服务帐户的另一种方式,在这种情况下我们可以创建活动但无法阻止房间。