Код: Выделить всё
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Код: Выделить всё
package utility.common;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;
import java.io.*;
import java.util.Arrays;
import java.util.List;
public class ReadGS {
private static final String APPLICATION_NAME = "Google Drive API Java Quickstart";
private static final java.io.File DATA_STORE_DIR = new java.io.File("src/main/resources/credentials");
private static final String RESOURCE_PATH = "credentials.json";
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
private static final List SCOPES = Arrays.asList(
SheetsScopes.SPREADSHEETS,
DriveScopes.DRIVE
);
private static Sheets sheetsService;
private static FileDataStoreFactory DATA_STORE_FACTORY;
private static HttpTransport HTTP_TRANSPORT;
static {
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
}
}
public static Credential authorize() throws Exception {
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
JSON_FACTORY,
new InputStreamReader(new FileInputStream(RESOURCE_PATH))
);
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT,
JSON_FACTORY,
clientSecrets,
SCOPES
).setDataStoreFactory(DATA_STORE_FACTORY).build();
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
private static Drive getDriveService() throws Exception {
Credential credential = authorize();
return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
public static void exportSheetAsXLSX(String fileId, String exportPath) throws Exception {
Drive driveService = getDriveService();
try {
File file = driveService.files().get(fileId).setFields("name, mimeType").execute();
String fileName = file.getName();
System.out.println("Exporting file: " + fileName);
try (InputStream inputStream = driveService.files().export(fileId, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet").executeMediaAsInputStream();
FileOutputStream outputStream = new FileOutputStream(exportPath)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
System.out.println("Google Sheet exported as XLSX successfully to: " + exportPath);
} catch (IOException e) {
System.err.println("Error writing to file " + exportPath + ": " + e.getMessage());
throw new IOException("Error exporting file " + fileId, e);
}
} catch (GoogleJsonResponseException e) {
System.err.println("Google API Error while accessing file: " + fileId + " - " + e.getDetails());
throw new Exception("Google API error during export", e);
} catch (IOException e) {
System.err.println("IO Error accessing file " + fileId + ": " + Arrays.toString(e.getStackTrace()));
throw new IOException("Error accessing file " + fileId, e);
} catch (Exception e) {
System.err.println("Unexpected error occurred during export: " + e.getMessage());
throw e;
}
}
}
Код: Выделить всё
com.google.api-client
google-api-client
1.33.0
com.google.oauth-client
google-oauth-client-jetty
1.33.1
com.google.apis
google-api-services-sheets
v4-rev614-1.18.0-rc
com.google.apis
google-api-services-drive
v3-rev197-1.25.0
Ошибка возникает, когда я пытаюсь получить доступ к API Google Диска для экспорта таблицы Google в XLSX:
Код: Выделить всё
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Шаги, которые я уже пробовал:
Обновлены зависимости Java и Maven.
Проверено подключение к Интернету.
Попытка вручную добавить отсутствующий сертификат в хранилище доверенных сертификатов Java.
Среда:
Ява 11
Maven
Клиентские библиотеки API Google (Google Диск, Google Таблицы)
Кто-нибудь сталкивался с этой ошибкой раньше или имеет какие-либо предложения по ее устранению?
Любая помощь будет оценена по достоинству!
Подробнее здесь: https://stackoverflow.com/questions/793 ... -requested