2017-03-01 108 views
1

我想檢索日曆中的所有事件,然後按日曆對其進行分類。Google日曆從每個日曆中獲取所有事件

這是我到目前爲止。

public class Quickstart { 
/** Application name. */ 
private static final String APPLICATION_NAME = 
    "Google Calendar API Java Quickstart"; 

/** Directory to store user credentials for this application. */ 
private static final java.io.File DATA_STORE_DIR = new java.io.File(
    System.getProperty("user.home"), ".credentials/calendar-java-quickstart"); 

/** Global instance of the {@link FileDataStoreFactory}. */ 
private static FileDataStoreFactory DATA_STORE_FACTORY; 

/** Global instance of the JSON factory. */ 
private static final JsonFactory JSON_FACTORY = 
    JacksonFactory.getDefaultInstance(); 

/** Global instance of the HTTP transport. */ 
private static HttpTransport HTTP_TRANSPORT; 

/** Global instance of the scopes required by this quickstart. 
* 
* If modifying these scopes, delete your previously saved credentials 
* at ~/.credentials/calendar-java-quickstart 
*/ 
private static final List<String> SCOPES = 
    Arrays.asList(CalendarScopes.CALENDAR_READONLY); 

static { 
    try { 
     HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); 
     DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR); 
    } catch (Throwable t) { 
     t.printStackTrace(); 
     System.exit(1); 
    } 
} 

/** 
* Creates an authorized Credential object. 
* @return an authorized Credential object. 
* @throws IOException 
*/ 
public static Credential authorize() throws IOException { 
    // Load client secrets. 
    BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\User\\.credentials\\calendar-java-quickstart\\client_secret.json")); 
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, br); 

    // 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; 
} 

/** 
* Build and return an authorized Calendar client service. 
* @return an authorized Calendar client service 
* @throws IOException 
*/ 
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 main(String[] args) throws IOException { 
    // Build a new authorized API client service. 
    // Note: Do not confuse this class with the 
    // com.google.api.services.calendar.model.Calendar class. 
    com.google.api.services.calendar.Calendar service = 
     getCalendarService(); 
    String pageToken = null; 
    // List the next 10 events from the primary calendar. 
    DateTime now = new DateTime(System.currentTimeMillis()); 
    Events events = service.events().list("primary") 
     .setPageToken(pageToken) 
     .setMaxResults(10) 
     .setTimeMin(now) 
     .setOrderBy("startTime") 
     .setSingleEvents(true) 
     .execute(); 
    List<Event> items = events.getItems(); 
    do 
    { 
     CalendarList calendarList = service.calendarList().list().setPageToken(pageToken).execute(); 
     List<CalendarListEntry> items2 = calendarList.getItems(); 

     for (CalendarListEntry calendarListEntry : items2) 
     { 
      System.out.println(calendarListEntry.getSummary()); 
     }//for 
      while(pageToken != null) 
      pageToken = calendarList.getNextPageToken(); 
    }//do 
    while (pageToken != null); 
    System.out.println("------------------------------"); 
    if (items.size() == 0) 
    { 
     System.out.println("No upcoming events found."); 
    }//if 
    else 
    { 
     for (Event event : items) 
     { 
      DateTime start = event.getStart().getDateTime(); 
      if (start == null) 
      { 
       start = event.getStart().getDate(); 
      }//if 
      System.out.println(event.getSummary()); 
     }//for 
    }//else 
}//main 

正如你所看到的,我可以得到所有的事件(從主日曆)和可用日曆的所有名稱。

如何獲取calendarId,以便按日曆排序事件?

回答

1

日曆列表返回clandarListResponse

{ 
    "kind": "calendar#calendarList", 
    "etag": etag, 
    "nextPageToken": string, 
    "nextSyncToken": string, 
    "items": [ 
    calendarList Resource 
    ] 
} 

您已經循環艱難每個calendarListEntry但不是做你的總結,你應該只要求你在這種情況下,ID所需的字段。

每個calendar#calendarListEntry都有一個id值,它是您正在查找的日曆標識。

我不是一個Java開發人員,但我猜的東西IKE

for (CalendarListEntry calendarListEntry : items) { 
    System.out.println(calendarListEntry.getId()); 
} 
+1

太謝謝你了! – Undefined

相關問題