8

這是使用此代碼我試圖用客房內設施與空間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(); 

} 
+0

您是否使用服務帳戶或Oauth2類很難告訴Java看起來像oauth2 – DaImTo

+0

是的,我正在使用服務帳戶@DalmTo –

+0

@DalmTo對此的任何解決方案,請建議如果您有任何解決方案因爲我卡在它從很長時間 –

回答

0

大家好從谷歌搜索長後,我找到解決方案。

步驟來創建事件谷歌事件。

第一步:設置以下範圍授權API。

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

第二步:在授權請求許可管理和查看日曆,使用具有允許它。 和哪些將生成授權碼。

步驟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創建活動。

我曾嘗試使用服務帳戶在這種情況下,我們能夠創造活動,但無法阻止房的另一種方式。

2

您正在使用服務帳戶。你需要記住的是一個服務帳戶不是你。服務帳戶擁有自己的Google日曆帳戶主要是其主日曆。

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

這會將一個事件添加到服務帳戶主Google日曆,您無法在Web上直觀地看到該日曆。

你試過從你的代碼做了events.list這應該告訴你的服務的事件佔谷歌日曆。

如果你希望能夠看到這種視覺上,我建議你創建自己的個人谷歌日曆帳戶日曆,並通過與服務共享它授予其服務帳戶訪問帳戶的電子郵件地址。

我的博客文章service accounts

+0

當我們創建服務帳戶其詢問服務帳戶的名稱和服務帳戶的作用請建議我們需要填寫那裏@DalmTo –

+0

什麼你想要我不認爲這很重要。 – DaImTo

+0

@達爾姆同樣的事情,我正在做與A房創建的事件,但是當我嘗試在同一時間創建另一個事件,然後它顯示房間avilable,而不應該顯示或其應該與房間罷工A –