Skip to content

Commit 58648bf

Browse files
committed
feat: google API 기능 추가
1 parent b1d5388 commit 58648bf

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ dependencies {
4949
testImplementation 'org.springframework.security:spring-security-test'
5050
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
5151

52+
implementation 'com.google.apis:google-api-services-sheets:v4-rev516-1.23.0'
53+
implementation 'com.google.auth:google-auth-library-oauth2-http:0.20.0'
54+
55+
5256
implementation 'io.jsonwebtoken:jjwt-api:0.12.6'
5357
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.6'
5458
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.12.6'
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package dmu.dasom.api.domain.google.controller;
2+
3+
import dmu.dasom.api.domain.google.service.GoogleApiService;
4+
import lombok.RequiredArgsConstructor;
5+
import lombok.Value;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.web.bind.annotation.PostMapping;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestParam;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
import java.util.Collections;
13+
import java.util.List;
14+
15+
@RestController
16+
@RequestMapping("/google")
17+
@RequiredArgsConstructor
18+
public class GoogleController {
19+
20+
private final GoogleApiService googleApiService;
21+
22+
private static final String SPREADSHEET_ID = "1Vpu6_2raNvJN_GGg7aGBmzV4cXG1rCoizHl9v7kbG2o";
23+
private static final String RANGE = "A1";
24+
25+
@PostMapping("/write")
26+
public ResponseEntity<String> writeToSheet(@RequestParam String word){
27+
try{
28+
List<List<Object>> values = List.of(Collections.singletonList(word));
29+
30+
googleApiService.writeToSheet(SPREADSHEET_ID, RANGE, values);
31+
return ResponseEntity.ok("Data written successfully to the spreadsheet" + word);
32+
} catch (Exception e){
33+
e.printStackTrace();
34+
return ResponseEntity.internalServerError().body("Failed to write data to the spreadsheet" + e.getMessage());
35+
}
36+
}
37+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}

src/main/resources/application-credentials.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ jwt:
1919
secret: ${JWT_SECRET}
2020
access-token-expiration: ${JWT_ACCESS_TOKEN_EXPIRATION}
2121
refresh-token-expiration: ${JWT_REFRESH_TOKEN_EXPIRATION}
22+
google:
23+
credentials:
24+
path: ${GOOGLE_CREDENTIALS_PATH}

0 commit comments

Comments
 (0)