Android Google Calendar API 자격 증명 가져 오기 [중복]

Dec 21 2020

무엇보다도 Google Calendar API에 액세스 할 수있는 앱을 개발하고 싶습니다.

그러면 앱 사용자도 이벤트에 액세스 할 수 있어야합니다. 하지만 안타깝게도 내 코드로 액세스 할 수 없습니다. 또한 Google Calendar API Java Quickstart 의 지침을 따랐습니다 .

이것은 내 코드입니다.

public class GoogleCalendar {

    private static final String APPLICATION_NAME = "Google Calendar Quickstart";
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    private static final String TOKENS_DIRECTORY_PATH = "tokens";

    /**
     * Global instance of the scopes required by this quickstart.
     * If modifying these scopes, delete your previously saved saved tokens/ folder.
     */
    private static final List<String> SCOPES = Collections.singletonList(CalendarScopes.CALENDAR_READONLY);
    private static final String CREDENTIALS_FILE_PATH = "/credential.json";

    /**
     * Creates an authorized Credential objects.
     * @param HTTP_TRANSPORT The network HTTP Transport
     * @return An authorized Credential object.
     * @throws IOException If the client_id.jason file cannot be found.
     */
    private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
        // Load client secrets.
        InputStream in = GoogleCalendar.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
        if (in == null) {
            throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
        }
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();
        LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
        return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
    }

    public static void getAllData() throws GeneralSecurityException, IOException {
        // Build a new authorized API client service.
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                .setApplicationName(APPLICATION_NAME)
                .build();
        // List the next 10 event from the primary calendar.
        DateTime now = new DateTime(System.currentTimeMillis());
        Events events = service.events().list("primary")
                .setMaxResults(10)
                .setTimeMin(now)
                .setOrderBy("startTime")
                .setSingleEvents(true)
                .execute();
        List<Event> items = events.getItems();
        if (items.isEmpty()) {
            System.out.println("No upcoming events found.");
        } else {
            System.out.println("Upcoming events");
            for (Event event : items) {
                DateTime start = event.getStart().getDateTime();
                if (start == null) {
                    start = event.getStart().getDate();
                }
                System.out.printf("%s (%s)\n", events.getSummary(), start);
            }
        }
    }
}

그리고 이것은 내 오류 메시지입니다. java.security.KeyStoreException: JKS not found

다른 클래스의 모든 데이터 가져 오기 메서드를 다음과 같이 호출했습니다.

try {
    GoogleCalendar.getAllData();
} catch (GeneralSecurityException | IOException e) {
    e.printStackTrace();
}

누락 된 정보가있는 경우 알려 주시면 전달하겠습니다.

미리 감사드립니다.

답변

JayRathodRJ Dec 21 2020 at 18:00

이것은 알려진 문제입니다 .Google 문제 추적기 에서 답변을 찾을 수 있다고 생각합니다 .

그냥 교체

HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();

이것으로

HTTP_TRANSPORT = new com.google.api.client.http.javanet.NetHttpTransport()