diff --git a/src/main/java/dmu/dasom/api/domain/google/service/GoogleApiService.java b/src/main/java/dmu/dasom/api/domain/google/service/GoogleApiService.java index 921deba..08e190e 100644 --- a/src/main/java/dmu/dasom/api/domain/google/service/GoogleApiService.java +++ b/src/main/java/dmu/dasom/api/domain/google/service/GoogleApiService.java @@ -7,37 +7,44 @@ import com.google.api.services.sheets.v4.Sheets; import com.google.api.services.sheets.v4.model.UpdateValuesResponse; import com.google.api.services.sheets.v4.model.ValueRange; +import com.google.auth.Credentials; import com.google.auth.http.HttpCredentialsAdapter; import com.google.auth.oauth2.GoogleCredentials; import dmu.dasom.api.domain.common.exception.CustomException; import dmu.dasom.api.domain.common.exception.ErrorCode; +import lombok.RequiredArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Service; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; import java.util.Collections; import java.util.List; +@RequiredArgsConstructor @Service public class GoogleApiService { private static final Logger logger = LoggerFactory.getLogger(GoogleApiService.class); private static final String APPLICATION_NAME = "Recruit Form"; private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); - @Value("${google.credentials.file.path}") - private String credentialsFilePath; + @Value("${google.credentials.json}") + private String credentialsJson; private Sheets sheetsService; - // 해당 메소드는 sheets의 인스턴스를 얻는데 사용 + // Google Sheets API 서비스 객체를 생성하는 메소드 private Sheets getSheetsService() throws IOException, GeneralSecurityException{ if(sheetsService == null){ + ByteArrayInputStream credentialsStream = new ByteArrayInputStream(credentialsJson.getBytes(StandardCharsets.UTF_8)); GoogleCredentials credentials = GoogleCredentials - .fromStream(new ClassPathResource(credentialsFilePath).getInputStream()) + .fromStream(credentialsStream) .createScoped(Collections.singletonList("https://www.googleapis.com/auth/spreadsheets")); + sheetsService = new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, new HttpCredentialsAdapter(credentials)) .setApplicationName(APPLICATION_NAME) .build(); diff --git a/src/main/resources/application-credentials.yml b/src/main/resources/application-credentials.yml index d718107..e9a7ac3 100644 --- a/src/main/resources/application-credentials.yml +++ b/src/main/resources/application-credentials.yml @@ -21,6 +21,6 @@ jwt: refresh-token-expiration: ${JWT_REFRESH_TOKEN_EXPIRATION} google: credentials: - path: ${GOOGLE_CREDENTIALS_PATH} + json: ${GOOGLE_CREDENTIALS_JSON} spreadsheet: id: ${GOOGLE_SPREADSHEET_ID} \ No newline at end of file