C
C
ceramicthree2021-07-26 23:04:11
Google
ceramicthree, 2021-07-26 23:04:11

How to get access token for Google services API with OAuth 2.0 on Spring?

OAuth 2.0 configured, it seems to work. The user in the Security context is saved. The Google API documentation says that in order to create an object of a service, you need to pass an access-token to it, but how can you get this token from? At the moment, the application yaml for the OAuth configuration and the WebSecurityConfig itself are described:
Web Security Config

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .oauth2Login();
    }
}

Application.yaml
spring:
  security:
    oauth2:
      client:
        provider:
          google:
            client-id: ****
            client-secret: ****
            scope:
            - email
            - profile
            - https://www.googleapis.com/auth/calendar


How now in the method to get his credentials or access-token after user authentication? Casting Credential from SecurityContext to Credential presented in Google libraries did not work(
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Credential credential = (Credential) authentication.getCredentials();

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question