A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://stackoverflow.com/questions/77731168/i-have-a-question-regarding-google-calendar-api-logic below:

java - I have a question regarding Google Calendar API logic

I'm testing Google Calendar authentication-related logic in Java. I'm not sure if I'm misunderstanding the concept, but I've been unable to resolve the issue for several days now.

My code is as follows:


GoogleAPI.java

    private static final Logger LOGGER = LoggerFactory.getLogger(GoogleAPI.class);
    
    private static final String APPLICATION_NAME = "Your Application Name";
    private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
    private static final String TOKENS_DIRECTORY_PATH = EgovProperties.getProperty("Globals.google.api.calendar.token");
    private static final String CLIENT_SECRETS_FILE_PATH = EgovProperties.getProperty("Globals.google.api.calendar");

    public static GoogleAuthorizationCodeFlow FLOW;
    public static LocalServerReceiver RECEIVER;
    
    public static Credential getCredential() throws IOException, GeneralSecurityException {
        
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        
        InputStream in = GoogleAPI.class.getResourceAsStream(CLIENT_SECRETS_FILE_PATH);
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        FLOW = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets,
                Collections.singletonList(CalendarScopes.CALENDAR))
                .setDataStoreFactory(new FileDataStoreFactory(new File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();

        RECEIVER = new LocalServerReceiver.Builder().setPort(8888).build();
        
        return new AuthorizationCodeInstalledApp(FLOW, RECEIVER).authorize("user");
    }

    public static List<TodolistVO> listEvents(String calendarId) throws GeneralSecurityException, IOException {
        
        List<TodolistVO> rtnList = new ArrayList<TodolistVO>();
        
        try {

            Calendar service = new Calendar.Builder(new NetHttpTransport(), JSON_FACTORY, getCredential())
                    .setApplicationName(APPLICATION_NAME)
                    .build();

            Events events = service.events().list(calendarId).execute();
            List<Event> items = events.getItems();

            
            for(Event evt : items) {
                
                TodolistVO vo = new TodolistVO();
                
                vo.setTitle(evt.getSummary());
                
            }
            
        } catch (Exception e) {

            e.printStackTrace();
            
        }
        
        return rtnList;
    }

The situation is as follows:

  1. The code is called. - listEvents()
  2. The Google authentication page opens. - getCredential()
  3. Without completing the authentication, it is closed.
  4. The code is called again.
  5. An error occurs, stating that the port is already in use.

There is another situation here:

  1. Call the code.
  2. The Google authentication page opens.
  3. Complete the authentication.
  4. Delete the authenticated information from Google's third-party apps and services.
  5. Call the code again.
  6. The authentication page doesn't open, and a token-related error occurs.
  7. Delete the StoredCredential file stored locally or on the server.
  8. Call the code again.
  9. The authentication page opens successfully.

It works when I delete the file, but something doesn't work correctly when the file is still present. How can I resolve this issue?

Please let me know if you need further assistance with this problem.

Using ChatGPT for translation might make the question content seem unusual at times.

I would really appreciate it if you could answer.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4