2

I would like to develop an app that will access the Google Calendar API, among other things.

The users of the app should then also be able to access the events. But unfortunately I can't get access with my code. I also followed the instruction from Google Calendar API Java Quickstart.

This is my code:

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);
            }
        }
    }
}

And this is my error message: java.security.KeyStoreException: JKS not found

I called the get all data method in a another class with:

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

If you have any questions of information is missing, please let me know and I will pass on the information.

Thank you in advance.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Stephan
  • 21
  • 1

1 Answers1

0

This is known issue i think you can found an answer on google issue tracker.

Just Replace This

HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();

With This

HTTP_TRANSPORT = new com.google.api.client.http.javanet.NetHttpTransport()
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
  • Please dont copy existing answers and reuse them, just duplicate the question. – Linda Lawton - DaImTo Dec 21 '20 at 11:51
  • @DaImTo I came across same issue while i was shading some lights on this topic. And at that moment i came across this link and i am having bookmarked still. I have not looked for other same answers if given here in SO to copy and paste here. Every same looked answer isn't copied pasted. – Jay Rathod Dec 21 '20 at 14:04