| 
7 | 7 | import com.google.api.services.sheets.v4.Sheets;  | 
8 | 8 | import com.google.api.services.sheets.v4.model.UpdateValuesResponse;  | 
9 | 9 | import com.google.api.services.sheets.v4.model.ValueRange;  | 
 | 10 | +import com.google.auth.Credentials;  | 
10 | 11 | import com.google.auth.http.HttpCredentialsAdapter;  | 
11 | 12 | import com.google.auth.oauth2.GoogleCredentials;  | 
12 | 13 | import dmu.dasom.api.domain.common.exception.CustomException;  | 
13 | 14 | import dmu.dasom.api.domain.common.exception.ErrorCode;  | 
 | 15 | +import lombok.RequiredArgsConstructor;  | 
14 | 16 | import org.slf4j.Logger;  | 
15 | 17 | import org.slf4j.LoggerFactory;  | 
16 | 18 | import org.springframework.beans.factory.annotation.Value;  | 
17 | 19 | import org.springframework.core.io.ClassPathResource;  | 
18 | 20 | import org.springframework.stereotype.Service;  | 
19 | 21 | 
 
  | 
 | 22 | +import java.io.ByteArrayInputStream;  | 
20 | 23 | import java.io.IOException;  | 
 | 24 | +import java.nio.charset.StandardCharsets;  | 
21 | 25 | import java.security.GeneralSecurityException;  | 
22 | 26 | import java.util.Collections;  | 
23 | 27 | import java.util.List;  | 
24 | 28 | 
 
  | 
 | 29 | +@RequiredArgsConstructor  | 
25 | 30 | @Service  | 
26 | 31 | public class GoogleApiService {  | 
27 | 32 | 
 
  | 
28 | 33 |     private static final Logger logger = LoggerFactory.getLogger(GoogleApiService.class);  | 
29 | 34 |     private static final String APPLICATION_NAME = "Recruit Form";  | 
30 | 35 |     private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();  | 
31 |  | -    @Value("${google.credentials.file.path}")  | 
32 |  | -    private String credentialsFilePath;  | 
 | 36 | +    @Value("${google.credentials.json}")  | 
 | 37 | +    private String credentialsJson;  | 
33 | 38 |     private Sheets sheetsService;  | 
34 | 39 | 
 
  | 
35 |  | -    // 해당 메소드는 sheets의 인스턴스를 얻는데 사용  | 
 | 40 | +    // Google Sheets API 서비스 객체를 생성하는 메소드  | 
36 | 41 |     private Sheets getSheetsService() throws IOException, GeneralSecurityException{  | 
37 | 42 |         if(sheetsService == null){  | 
 | 43 | +            ByteArrayInputStream credentialsStream = new ByteArrayInputStream(credentialsJson.getBytes(StandardCharsets.UTF_8));  | 
38 | 44 |             GoogleCredentials credentials = GoogleCredentials  | 
39 |  | -                    .fromStream(new ClassPathResource(credentialsFilePath).getInputStream())  | 
 | 45 | +                    .fromStream(credentialsStream)  | 
40 | 46 |                     .createScoped(Collections.singletonList("https://www.googleapis.com/auth/spreadsheets"));  | 
 | 47 | + | 
41 | 48 |             sheetsService = new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, new HttpCredentialsAdapter(credentials))  | 
42 | 49 |                     .setApplicationName(APPLICATION_NAME)  | 
43 | 50 |                     .build();  | 
 | 
0 commit comments