javaspring-bootgoogle-oauthgoogle-calendar-apigoogle-api-java-client

google-calendar-api with java spring boot


I am developing a java Springboot web application. In Which I have multiple users that login with a adress mail and a password. The login is Ok.

@Override
protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().antMatchers("/error.xhtml").permitAll().antMatchers("/error-404.xhtml").permitAll()
            .anyRequest().authenticated().accessDeniedPage("/accessDenied.xhtml");
    ;
    // login
    http.formLogin().loginPage("/login").permitAll().failureUrl("/login?error=1");
    // logout
    http.logout().logoutSuccessUrl("/login").invalidateHttpSession(true).deleteCookies("JSESSIONID");

    http.formLogin().defaultSuccessUrl("/dashboard", true);

    http.csrf().disable();
}

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}

@Override
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
    authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

I don’t want a google login. I need to integrate their google calendar to application account so hey cant creat event and meeting using their gmail id.

Is there any way that I can integrate their google account to the application by having to get consent from the users just only one time in the setting.

enter image description here


Solution

  • You should have a look at the official sample

    // 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();
    

    If FileDataStoreFactory is configured properly it will store each users credentials, then you will only need to request access once.

    Update:

    questions from comments

    1.how to configured properly the FileDataStoreFactory store each users credentials and and use those of the connected user?

    Check the sample below .authorize("user"); will denote a user and store the creds for that user.

    1. how to disconect an account?

    by either calling the revoke endpoint or deleting their file stored above. It will force your app to request authorization again.

    private static Credential authorize() throws Exception
        {
            HttpTransport httpTransport = new NetHttpTransport();
            JsonFactory jsonFactory = new JacksonFactory();
    
            // load client secrets
            GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
                    new InputStreamReader(BigQueryConsumer.class.getResourceAsStream("/secret.json")));
    
            // This creates the credentials datastore at ~/.oauth-credentials/${credentialDatastore}
            FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY));
    
            // set up authorization code flow
            GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                    httpTransport, JSON_FACTORY, clientSecrets,
                    SCOPES).setDataStoreFactory(fileDataStoreFactory)
                    .build();
            // authorize
            return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
        }