2015-12-14 59 views
0

在遵循developers.google.com中的指导后,我已成功地读取,插入或删除属于单个Google帐户的一个或多个Google日历的事件。现在,我想为使用该方法的多个帐户执行相同操作。如何在Java中的多个Google帐户中管理日历

以下是一些更多信息的情况。 在这种情况下,我在Google Developers Console中创建了一个项目并打开了API。然后我创建了OAuth 2.0 client ID以通过同意屏幕对用户进行身份验证。

然后我尝试使用下载的客户机密JSON来连接日历。在这种情况下,我被给了一个URL来显示同意屏幕并获得如下所述的用户认证。 enter image description here

一切顺利,我可以成功读取,插入或删除属于Google帐户(最初通过身份验证)的一个或多个Google日历的事件。

现在,我想访问一些其他谷歌账户,并管理他们的日历。在这种情况下,对于其他帐户,我也通过URL获得了成功的验证码,以显示如下所述的同意屏幕。

http://localhost:12345/Callback?code=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
收到验证码。您现在可以关闭此窗口...

但我不知道如何使用它与Java程序来管理这些Ca贷方属于不同的谷歌帐户。它不断管理第一个认证账户的日历。

如何做到这一点?基本上如何使用上面提到的developers.google.com指南中提到的方法通过java在多个Google帐户中管理日历。

代码:

private static void viewEvents() 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("Upcoming 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); 
      }  
     } 
    } 

    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 Credential authorize() throws IOException { 

     InputStream in = CalendarQuickstart.class.getResourceAsStream("/client_secret_1.0.0.json"); 
     GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); 

     GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) 
                       .setDataStoreFactory(DATA_STORE_FACTORY) 
                       .setAccessType("online") 
                       .build(); 

     Credential credential = null; 
     try {credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");} catch (Exception e) {} 

     System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath()); 

     return credential; 
    } 

回答

0

我用我自己找到了另一种方式回答。

Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); 

我们可以通过使用authorize()方法的String参数来做到这一点。 (例如:“用户”)。

比方说,要在两个帐户[email protected][email protected]管理压延,

  1. 执行autherize()方法给它的参数“namal”。然后,您将获得URL以获得许可屏幕。
  2. 使用[email protected]帐户批准它。它将针对关键“namal”存储[email protected]的凭证。
  3. 执行authorize()方法,它给出参数“lakmini”。然后,您将获得URL以获得许可屏幕。
  4. [email protected]注销或将URL发送到已记录为“[email protected]”的浏览器。
  5. 使用[email protected]帐户批准它。它将针对关键“lakmini”存储[email protected]的凭证。
  6. 现在,当您使用“namal”键时,您将连接到[email protected]的日历。当你使用密钥“lakmini”时,你将连接到日历[email protected]
相关问题