8

To jest mój kod Java za pomocą tego kodu Próbuję utworzyć zdarzenie z pokoju (pokój jest dodawany przy użyciu zasobu Google Calendar API) zdarzenie utworzone sukces w pełni z pokoju A. Jednak po odprawie Kalendarz Google i spróbuj zobaczyć dostępny pokój w tym, że pokój jest dostępny. Spodziewałbym się, że nie powinien się wyświetlać lub powinien pokazywać się z ostrzeżeniem, czy ktoś może mi powiedzieć, jakie jest rozwiązanie tego problemu. Czyli popełniam błąd, czy jest problem z uprawnieniami, proszę zasugeruj mi.Nie można zablokować pokoju przy użyciu kalendarza 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(); 

} 
+0

Czy używasz konta usługi lub typu Oauth2 trudno powiedzieć z Java wygląda jak oauth2 – DaImTo

+0

tak, używam konta usługi @ DalmTo –

+0

@ DalmTo jakiekolwiek rozwiązanie do tego proszę zasugerować, jeśli masz jakieś rozwiązanie, bo jestem utknąć w to od długiego czasu –

Odpowiedz

0

Witam wszystkich po długich poszukiwaniach z google znalazłem rozwiązanie.

Kroki, aby utworzyć wydarzenie google.

Krok 1: Ustaw następujące zakresy, aby autoryzować api.

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

Krok 2: Podczas zatwierdzający prosi o pozwolenie na zarządzanie i widok kalendarza, wykorzystuje ma na to pozwolić. i który wygeneruje kod autoryzacji.

Krok 3: Tworzenie access_token kodem autoryzacji generowane

Krok 4: Hasło generowane access_token do craete zdarzenie Google.

kod Java do tworzenia google zdarzenie

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

} 

te etapy pracy dla mnie pokój bloku przy tworzeniu Event za pomocą interfejsu API Google Calendar.

Próbowałem w inny sposób, korzystając z konta usługi, w takim przypadku jesteśmy w stanie stworzyć wydarzenie, ale nie możemy zablokować miejsca.

2

Używasz konta usługi. Musisz pamiętać, że konto usługi NIE jest Tobą. Konta usług mają własne konto kalendarza Google Podstawowy jest jego głównym kalendarzem.

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

Zamierza to dodać wydarzenie do głównego kalendarza Google Usług Google, którego nie widać wizualnie w Internecie.

Czy próbowałeś zrobić events.list z kodu, który powinien pokazać zdarzenia na koncie usługi google kalendarz.

Jeśli chcesz widzieć to wizualnie, sugerujemy utworzenie kalendarza na osobistym koncie Kalendarza Google i przyznanie dostępu do konta usługi przez udostępnienie go adresowi e-mail konta usługi.

Mój blog post o service accounts

+0

kiedy tworzymy konto usługi, pytając nazwę konta usługi i rolę konta usługi, sugerujemy to, czego potrzebujemy wypełnić @DalmTo –

+0

Czegokolwiek chcesz, nie uważam, że to ma znaczenie. – DaImTo

+0

@DalmTo samo robię wydarzenie utworzone w pokoju A, ale kiedy próbuję stworzyć kolejne wydarzenie w tym samym czasie, to pokazuje pokój A jest dostępny podczas gdy nie powinien się wyświetlać lub powinien uderzać z pokoju A –

Powiązane problemy