ÃֽŠ°Ô½Ã±Û(JAVA)
2020.03.20 / 09:57

JAVA¸¦ ÀÌ¿ëÇÑ ±¸±Û Ķ¸°´õ API ¿¬µ¿

Äڷγª
Ãßõ ¼ö 229

¿ÜºÎ¿¡¼­ Á¦ÀÛÇÑ È¸ÀÇ½Ç ¿¹¾àÀ» °ü¸®ÇÏ´Â ¼­ºñ½º¸¦ »ç¿ëÁßÀÌ´Ù.

ÀÌ ¼­ºñ½º¿¡¼­ ¿¹¾àÇÑ È¸ÀÇ 30ºÐ Àü¿¡ ȸÀÇ Âü°¡Àڵ鿡°Ô ¾Ë¸²À» ÁÖ°í ½Í´Ù´Â ¿ä±¸»çÇ×ÀÌ »ý°å´Ù.

¿ÜºÎ¿¡¼­ Á¦ÀÛÇÑ ¼­ºñ½ºÀ̱⠶§¹®¿¡ Á÷Á¢ ¼­ºñ½º¿¡ ±â´ÉÀ» Ãß°¡ÇÒ ¼ö ¾ø´Â »óȲÀ̾ú´Ù.

 

´Ù¸¸ ¼­ºñ½º¿¡¼­ ȸÀÇ½Ç ¿¹¾à Á¤º¸¸¦ ÀúÀåÇÏ°í ÀÖ´Â DBÀÇ Å×À̺í°ú ÀÚ·áÀÇ Çü½ÄÀº ¾Ë°íÀÖ¾ú´Ù.

±×·¡¼­ ÇØ´ç Å×À̺íÀ» °¨½ÃÇϸ鼭 ´Ù°¡¿Â ȸÀÇ½Ç ¿¹¾à¿¡ ´ëÇØ ¾Ë¸²À» Á¦°øÇÏ´Â ÇÁ·Î±×·¥À» ¸¸µé±â·Î ÇÏ¿´´Ù.

 

¿¹¾à Á¤º¸´Â DB¿¡¼­ °¡Á®¿ÔÁö¸¸, ÀÌ°É ¾Ë¸²À» ÁÖ·Á¸é ¹»·Î ÇؾßÇÒÁö °í¹ÎÀÌ µÇ¾ú´Ù.

½±°Ô´Â ÅÚ·¹±×·¥ °°Àº ¸Þ½ÅÀú·Î º¸³»µµ µÇ°í, »ç³»¿¡¼­ »ç¿ëÇÏ´Â ·ÎÄÏêÀ» ÀÌ¿ëÇصµ µÈ´Ù.

¿©·¯°¡Áö¸¦ °í¹ÎÇÏ´Ù°¡ ±¸±ÛĶ¸°´õ¿¡ ÀÏÁ¤À¸·Î µî·ÏÇØÁÖ¸é ÀÌ ¿¹¾àÁ¤º¸¸¦ ´Ù¾çÇÏ°Ô È°¿ëÀÌ °¡´ÉÇÏ°Ú´Ù´Â »ý°¢À» ÇÏ°Ô µÇ¾ú´Ù.

 

±¸±ÛĶ¸°´õ¿¡ ÀÏÁ¤À¸·Î µî·ÏÀ» ÇϰԵǸé..

  • ÀÏÁ¤ÀÌ µî·ÏµÈ ¼ø°£ »ç¿ëÀÚÀÇ ±¸±Û°èÁ¤ ÀÏÁ¤¿¡ µî·ÏÀÌ µÇ°í ¾Û¿¡¼­ µî·Ï ¾Ë¸²À» ÁØ´Ù.
  • »ç¿ëÀÚ°¡ ÀÚ½ÅÀÇ ÀÏÁ¤¾Û¿¡¼­ ÀÏÁ¤ ½ÃÀÛ ¸îºÐÀü¿¡ ¾Ë¸²À» ÁÙÁö Á¤ÇÒ ¼ö ÀÖ´Ù(±âº»°ªÀº 30ºÐ, 10ºÐ)
  • ÈÞ´ëÆùÀÇ ÀÏÁ¤¾Û »Ó ¾Æ´Ï¶ó ÄÄÇ»ÅÍÀÇ ºê¶ó¿ìÀú¿¡¼­µµ ÀÏÁ¤ ±â´ÉÀ» Å°¸é È®ÀÎÀÌ °¡´ÉÇÏ´Ù.

±¸±ÛĶ¸°´õ¸¦ µµÀÔ ÇÏ·Á¸é ¾Ë¸²À» ¹ÞÀ» »ç¶÷µéÀÇ ±¸±Û°èÁ¤ÀÌ ÇÊ¿äÇÏ´Ù.

¿äÁò ´ëºÎºÐ ±¸±Û°èÁ¤ ÇÑ°³¾¿ °¡Áö°í Àֱ⶧¹®¿¡ Å« Á¦¾àÀº ¾Æ´Ï¾ú´Ù.

ÀÌ Á¡¸¸ ÇÕÀÇ°¡ µÈ´Ù¸é ¿ø·¡ ¿ä°ÇÀ» ÃæÁ·ÇÏ°í Ãß°¡·Î ÆíÀǼºÀ» ´õ º¸ÀåÇÒ ¼ö À־ ±¸±ÛĶ¸°´õ¿¡ ȸÀÇ½Ç ¿¹¾à Á¤º¸¸¦ µî·ÏÇϱâ·Î ÇÏ¿´´Ù.

 

±¸±ÛĶ¸°´õ API ¿Í ¿¬µ¿ÇÏ·Á¸é ±¸±Û°³¹ßÀÚ °èÁ¤°ú API µî·Ï°úÁ¤ÀÌ ÇÊ¿äÇÏ´Ù.

¶ÇÇÑ JAVA¿¡¼­ OAuth2 ÀÎÁõÀ» À§ÇØ ÀÎÁõÁ¤º¸¸¦ ´ãÀº json ÆÄÀÏÀÌ ÇÊ¿äÇÏ´Ù.

¼³Á¤°úÁ¤Àº Á¤¸®°¡ Àß µÈ ¸µÅ©¸¦ ÷ºÎÇÑ´Ù. ¾Æ·¡ ¸µÅ©¿¡¼­ json ÆÄÀÏÀ» ´Ù¿î¹Þ´Â °÷ ±îÁö ÁøÇàÇÏ¸é µÈ´Ù.

https://kingbbode.tistory.com/8

 

 

°³¹ßÀº SpringBoot 2, Maven ȯ°æ¿¡¼­ ÁøÇàÇÏ¿´´Ù.

¿¬µ¿ºÎºÐ¿¡ ´ëÇÑ ÄÚµå´Â µüÈ÷ ½ºÇÁ¸µÀÇ ¶óÀ̺귯¸®¸¦ »ç¿ëÇÏÁö ¾Ê¾ÒÀ¸´Ï ½ºÇÁ¸µÀÌ ²À ÇÊ¿äÇÏÁö´Â ¾Ê´Ù.

 

±¸±Û oauth ÀÎÁõÀ» Çϱâ À§Çؼ­´Â ¾Æ·¡ ¶óÀ̺귯¸® µðÆæ´ø½Ã¸¦ Ãß°¡ ÇؾßÇÑ´Ù.

 

<dependency>
   <groupId>com.google.api-client</groupId>
   <artifactId>google-api-client</artifactId>
   <version>1.23.0</version>
</dependency>
<dependency>
   <groupId>com.google.oauth-client</groupId>
   <artifactId>google-oauth-client-jetty</artifactId>
   <version>1.23.0</version>
</dependency>
<dependency>
   <groupId>com.google.apis</groupId>
   <artifactId>google-api-services-calendar</artifactId>
   <version>v3-rev305-1.23.0</version>
</dependency>

 

¾Æ·¡´Â ±¸±ÛĶ¸°´õ API ¸¦ ÀÌ¿ëÇØ ÀÏÁ¤À» µî·ÏÇÏ°í »èÁ¦ÇÏ´Â ³»¿ëÀ» ´ãÀº ÄÚµåÀÌ´Ù.

public class GoogleCalendar {



   private static final String APPLICATION_NAME = "Google Calendar API Java Quickstart";

   private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

   private static final String CREDENTIALS_FOLDER = "credentials"; // Directory to store user credentials.

   private static final String CALENDAR_ID = "[Ķ¸°´õ¿¬µ¿¾ÆÀ̵ð]";



   /**

    * Global instance of the scopes required by this quickstart.

    * If modifying these scopes, delete your previously saved credentials/ folder.

    */

   private static final List<String> SCOPES = Collections.singletonList(CalendarScopes.CALENDAR);

   private static final String CLIENT_SECRET_DIR = "/client_secret.json";





   public static Event addEvent(Event event) throws IOException, GeneralSecurityException, GoogleJsonResponseException {

       final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();

       Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))

               .setApplicationName(APPLICATION_NAME)

               .build();

       return service.events().insert(CALENDAR_ID, event).execute();

   }



   public static void delEvent(String eventKey) throws IOException, GeneralSecurityException, GoogleJsonResponseException {

       final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();

       Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))

               .setApplicationName(APPLICATION_NAME)

               .build();

       service.events().delete(CALENDAR_ID, eventKey).execute();

   }



   /**

    * Creates an authorized Credential object.

    * @param HTTP_TRANSPORT The network HTTP Transport.

    * @return An authorized Credential object.

    * @throws IOException If there is no client_secret.

    */

   private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {

       // Load client secrets.

       InputStream in = GoogleCalendar.class.getResourceAsStream(CLIENT_SECRET_DIR);

       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(new FileDataStoreFactory(new java.io.File(CREDENTIALS_FOLDER)))

               .setAccessType("offline")

               .build();

       return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");

   }

}

getCredentials

OAuth ÀÎÁõÀ» ÀÌ¿ëÇØ ±¸±Û¿¡¼­ ÀÎÁõÀ» °¡Á®¿À´Â ¸Þ¼ÒµåÀÌ´Ù.

¾Õ¿¡¼­ ¸¸µç ÀÎÁõ jsonÀ» ºÒ·¯¿Í¾ß ÇÑ´Ù. ³ªÀÇ °æ¿ì´Â resource Æú´õ¿¡ ³Ö°í ºÒ·¯¿Ô´Ù.

ÀÌ ÀÎÁõ¸Þ¼Òµå´Â api ¿äû ¶§ ¸¶´Ù ½ÇÇàÇؼ­ ÀÎÁõÁ¤º¸¸¦ ¹ÝȯÇÑ´Ù.

 

addEvent

±¸±ÛĶ¸°´õ¿¡ ÀÏÁ¤À» Ãß°¡ÇÏ´Â ¸Þ¼ÒµåÀÌ´Ù.

ÀÏÁ¤Á¤º¸¸¦ ´ã±â À§ÇØ Event ¶ó´Â °´Ã¼¸¦ »ç¿ëÇÑ´Ù.

Event °´Ã¼¸¦ »ç¿ëÇÏ´Â ¹æ¹ýÀº ¾Æ·¡¿¡ µû·Î ´Ù·ç¾úÀ¸´Ï Âü°íÇÏÀÚ.

 

delEvent

±¸±ÛĶ¸°´õ¿¡ ÀÏÁ¤À» »èÁ¦ÇÏ´Â ¸Þ¼ÒµåÀÌ´Ù.

addEvent ÇßÀ»¶§ ±¸±Û¿¡¼­ ÀÀ´ä¹ÞÀº eventKey °¡ ÇÊ¿äÇÏ´Ù.

 

Event »ý¼º

À̺¥Æ® »ý¼ºÀº Å©°Ô Á¦¸ñ, ½ÃÀ۽ð£, Á¾·á½Ã°£, Âü¿©ÀÚ Á¤º¸°¡ ÇÊ¿äÇÏ´Ù. ¾Æ·¡´Â ÀÏÁ¤À» Ãß°¡ÇÏ´Â ¸Þ¼ÒµåÀÌ´Ù.

Âü¿©ÀÚÁ¤º¸(ResourceSubscriber), ÀÏÁ¤Á¤º¸(RecourceInfo), »ç¿øÁ¤º¸(EmpInfo) ´Â ³ªÀÇ ½Ã½ºÅÛ¿¡¼­ »ç¿ëÇÏ´Â DB¿¡ ¸Â°Ô Á÷Á¢ ¸¸µç °´Ã¼ÀÌ´Ù. Âü¿©ÀÚÁ¤º¸, ÀÏÁ¤Á¤º¸, »ç¿øÁ¤º¸ ºÎºÐÀº º»ÀÎÀÇ ½Ã½ºÅÛ¿¡ ¸Â°Ô °¢ÀÚ Á¦ÀÛÇÏ¸é µÈ´Ù.

 

public Event makeEvent(ResourceInfo resourceInfo, List<ResourceSubscriber> subList) throws ParseException, IOException, GeneralSecurityException {

   Event event = new Event()

           .setSummary(resourceInfo.getReqText())

           .setLocation(getMeetingRoom(resourceInfo.getResSeq()).getResName()) //Àå¼ÒÀ̸§Àº µû·Î ºÒ·¯¿À±æ..

           .setDescription(resourceInfo.getDescText());



   DateTime startDateTime = new DateTime(dateTimeTzFormat.format(dateTimeFormat.parse(resourceInfo.getStartDate())));

   EventDateTime start = new EventDateTime()

           .setDateTime(startDateTime)

           .setTimeZone("Asia/Seoul");

   event.setStart(start);



   DateTime endDateTime = new DateTime(dateTimeTzFormat.format(dateTimeFormat.parse(resourceInfo.getEndDate())));

   EventDateTime end = new EventDateTime()

           .setDateTime(endDateTime)

           .setTimeZone("Asia/Seoul");

   event.setEnd(end);



   EventReminder[] reminderOverrides = new EventReminder[]{

           new EventReminder().setMethod("popup").setMinutes(10),

   };

   Event.Reminders reminders = new Event.Reminders()

           .setUseDefault(false)

           .setOverrides(Arrays.asList(reminderOverrides));

   event.setReminders(reminders);



   List<EventAttendee> attendList = new ArrayList();



   for (ResourceSubscriber subObj : subList) {

       logger.info("SUB ADD OBJ : " + subObj.toString());

       EmpInfo empInfo = getEmpInfo(subObj.getEmpSeq());



       if (empInfo.getOutMail() != null && empInfo.getOutMail().length() > 0 && empInfo.getOutDomain() != null && empInfo.getOutDomain().length() > 0) {

           if (empInfo.getOutDomain().equals("gmail.com")) {

               attendList.add(new EventAttendee().setEmail(empInfo.getOutMail() + "@" + empInfo.getOutDomain()));

           } else {

               //À̸ÞÀÏÁÖ¼Ò°¡ gmail.com ÀÌ ¾Æ´Ñ ÄÉÀ̽º. ÀÏ´ÜÀº ¾Æ¹«°Íµµ ¾ÈÇÔ.

           }

       } else {

           //¸ÞÀÏÁÖ¼Ò°¡ ¾ø´Â ÄÉÀ̽º.

       }

   }

   event.setAttendees(attendList);

   try {

       event = GoogleCalendar.addEvent(event);

   } catch (Exception ex) {

       logger.info("Calendar Exception in insert");

   }

   logger.info("EVENT : " + event.toPrettyString());



   return event;

}

 

±¸±Û Ķ¸°´õ API ¿¡´Â ´õ ´Ù¾çÇÑ ±â´ÉÀÌ ÀÖ´Ù. ¾Æ·¡ ¸µÅ©¿¡¼­ Âü°íÇÏÀÚ.

https://developers.google.com/calendar/v3/reference/

ºÒ·¯¿À´Â ÁßÀÔ´Ï´Ù...