|
| 1 | +package dmu.dasom.api.domain.google.service; |
| 2 | + |
| 3 | +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; |
| 4 | +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; |
| 5 | +import com.google.api.client.json.JsonFactory; |
| 6 | +import com.google.api.client.json.jackson2.JacksonFactory; |
| 7 | +import com.google.api.client.util.Value; |
| 8 | +import com.google.api.services.sheets.v4.Sheets; |
| 9 | +import com.google.api.services.sheets.v4.model.UpdateValuesResponse; |
| 10 | +import com.google.api.services.sheets.v4.model.ValueRange; |
| 11 | +import com.google.auth.http.HttpCredentialsAdapter; |
| 12 | +import com.google.auth.oauth2.GoogleCredentials; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | +import org.springframework.core.io.ClassPathResource; |
| 16 | +import org.springframework.stereotype.Service; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.security.GeneralSecurityException; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +@Service |
| 24 | +public class GoogleApiService { |
| 25 | + |
| 26 | + private static final Logger logger = LoggerFactory.getLogger(GoogleApiService.class); |
| 27 | + private static final String APPLICATION_NAME = "Recruit Form"; |
| 28 | + private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); |
| 29 | + @Value("${google.credentials.file.path}") |
| 30 | + private String credentialsFilePath; |
| 31 | + private Sheets sheetsService; |
| 32 | + |
| 33 | + // 해당 메소드는 sheets의 인스턴스를 얻는데 사용 |
| 34 | + private Sheets getSheetsService() throws IOException, GeneralSecurityException{ |
| 35 | + if(sheetsService == null){ |
| 36 | + GoogleCredentials credentials = GoogleCredentials |
| 37 | + .fromStream(new ClassPathResource(credentialsFilePath).getInputStream()) |
| 38 | + .createScoped(Collections.singletonList("https://www.googleapis.com/auth/spreadsheets")); |
| 39 | + sheetsService = new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, new HttpCredentialsAdapter(credentials)) |
| 40 | + .setApplicationName(APPLICATION_NAME) |
| 41 | + .build(); |
| 42 | + } |
| 43 | + return sheetsService; |
| 44 | + } |
| 45 | + |
| 46 | + public void writeToSheet(String spreadsheetId, String range, List<List<Object>> values) { |
| 47 | + try { |
| 48 | + Sheets service = getSheetsService(); |
| 49 | + ValueRange body = new ValueRange().setValues(values); |
| 50 | + UpdateValuesResponse result = service.spreadsheets().values() |
| 51 | + .update(spreadsheetId, range, body) |
| 52 | + .setValueInputOption("USER_ENTERED") |
| 53 | + .execute(); |
| 54 | + logger.info("Updated rows: {}", result.getUpdatedRows()); |
| 55 | + } catch (IOException e) { |
| 56 | + logger.error("Failed to write data to the spreadsheet", e); |
| 57 | + throw new RuntimeException("Failed to write data to the spreadsheet: " + e.getMessage(), e); |
| 58 | + } catch (GeneralSecurityException e) { |
| 59 | + throw new RuntimeException(e); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments