Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-credentials.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}