无法使用谷歌日历API
这是使用此代码我试图用客房内设施与空间A.创建事件(资源使用谷歌日历API添加房间)事件创建成功,但是当我检查我的Java代码来阻止房Google日历并尝试查看可用空间。我希望它不应该显示或应该显示罢工可以任何人请告诉我这个解决方案在哪里做我错了是否有权限问题请建议我。无法使用谷歌日历API
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("[email protected]"),
new EventAttendee().setEmail("[email protected]"), new EventAttendee().
setEmail("[email protected]m").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();
}
大家好从谷歌搜索长后,我找到解决方案。
步骤来创建事件谷歌事件。
第一步:设置以下范围授权API。
第二步:在授权请求许可管理和查看日历,使用具有允许它。 和哪些将生成授权码。
步骤3:由生成的授权代码
步骤4创建ACCESS_TOKEN:通产生的access_token到谷歌了解创建事件。
Java代码来创建谷歌事件
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();
}
这些步骤对我的工作块房间,而使用谷歌日历API创建活动。
我曾尝试使用服务帐户在这种情况下,我们能够创造活动,但无法阻止房的另一种方式。
您正在使用服务帐户。你需要记住的是一个服务帐户不是你。服务帐户拥有自己的Google日历帐户主要是其主日历。
String calendarId = "primary";
event = getCalendarService().events().insert(calendarId, event).execute();
这会将一个事件添加到服务帐户主Google日历,您无法在Web上直观地看到该日历。
你试过从你的代码做了events.list这应该告诉你的服务的事件占谷歌日历。
如果你希望能够看到这种视觉上,我建议你创建自己的个人谷歌日历帐户日历,并通过与服务共享它授予其服务帐户访问帐户的电子邮件地址。
我的博客文章service accounts
当我们创建服务帐户其询问服务帐户的名称和服务帐户的作用请建议我们需要填写那里@DalmTo –
什么你想要我不认为这很重要。 – DaImTo
@达尔姆同样的事情,我正在做与A房创建的事件,但是当我尝试在同一时间创建另一个事件,然后它显示房间avilable,而不应该显示或其应该与房间罢工A –
您是否使用服务帐户或Oauth2类很难告诉Java看起来像oauth2 – DaImTo
是的,我正在使用服务帐户@DalmTo –
@DalmTo对此的任何解决方案,请建议如果您有任何解决方案因为我卡在它从很长时间 –