8

This is my java code using this code I am trying to create event with room (room is added using resource Google Calendar API) event created success fully with room A. However when I check in Google Calendar and try see available room in that A room is available. I would expect it should not display or it should show with strike can any one please tell me the solution for this where am doing I am mistake is there permission issue please suggest me.

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();

 }
Research Development
  • 884
  • 1
  • 19
  • 39

2 Answers2

2

You are using a service account. What you need to remember is that a service account is NOT you. Service accounts have their own Google calendar account Primary is its primary calendar.

String calendarId = "primary";
event = getCalendarService().events().insert(calendarId, event).execute();

This is going to add an event to the Service accounts primary Google Calendar which you can not see visually on the web.

Have you tried doing a events.list from your code this should show you the events on the service accounts google calendar.

If you want to be able to see this visually I suggest you create a calendar on your own personal Google Calendar account and grant your service account access to it by sharing it with the service accounts email address.

My blog post about service accounts

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
0

Hi All after long search from google i found solution .

Steps to create event google event.

Step1: Set following scopes to authorise api.

  1. https://www.googleapis.com/auth/calendar.readonly
  2. https://www.googleapis.com/auth/calendar

Step2: While authorizing asks for permission to manage and view calendar , uses has to allow it . and which will generated authorization code.

Step3: Create access_token by generated authorization code

Step 4: Pass generated access_token to craete google event.

Java code to create google event

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();

}

these steps Work for me block room while creating Event using Google calendar api.

i have tried with another way using service account in that case we are able to create event but not able to block room .

Research Development
  • 884
  • 1
  • 19
  • 39