title: Spring Boot OAuth2 - GoogleApi- Could not obtain user details from token
body: <p>I am working on a project which has requirements for Gmail Authentication and  also which can be extended.
  I was following this tutorial <a href="https://spring.io/guides/tutorials/spring-boot-oauth2/" rel="nofollow">here</a> which had examples for Facebook and GitHub authentication. So I tried for Gmail and I am getting this error which I am not able to resolve and getting new exceptions when trying to resolve. Kindly, help me out as I believe this is the point where the code is least affected by my additions. With this much of config and code, it is working for github and fb but not for google.</p>
  
  <p><strong>SocialApplication.java</strong></p>
  
  <pre><code>@SpringBootApplication
  @RestController
  @EnableOAuth2Client
  @EnableAuthorizationServer
  @Order(6)
  public class SocialApplication extends WebSecurityConfigurerAdapter {
  
      @Autowired
      OAuth2ClientContext oauth2ClientContext;
  
      @RequestMapping({ "/user", "/me" })
      public Map&lt;String, String&gt; user(Principal principal) {
          Map&lt;String, String&gt; map = new LinkedHashMap&lt;&gt;();
          map.put("name", principal.getName());
          return map;
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          // @formatter:off
          http.antMatcher("/**").authorizeRequests().antMatchers("/", "/login**", "/webjars/**").permitAll().anyRequest()
                  .authenticated().and().exceptionHandling()
                  .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/")).and().logout()
                  .logoutSuccessUrl("/").permitAll().and().csrf()
                  .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).and()
                  .addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
          // @formatter:on
      }
  
      @Configuration
      @EnableResourceServer
      protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
          @Override
          public void configure(HttpSecurity http) throws Exception {
              // @formatter:off
              http.antMatcher("/me").authorizeRequests().anyRequest().authenticated();
              // @formatter:on
          }
      }
  
      public static void main(String[] args) {
          SpringApplication.run(SocialApplication.class, args);
      }
  
      @Bean
      public FilterRegistrationBean oauth2ClientFilterRegistration(OAuth2ClientContextFilter filter) {
          FilterRegistrationBean registration = new FilterRegistrationBean();
          registration.setFilter(filter);
          registration.setOrder(-100);
          return registration;
      }
  
      @Bean
      @ConfigurationProperties("github")
      ClientResources github() {
          return new ClientResources();
      }
  
      @Bean
      @ConfigurationProperties("facebook")
      ClientResources facebook() {
          return new ClientResources();
      }
  
      **@Bean
      @ConfigurationProperties("gmail")
      ClientResources gmail(){return new ClientResources();}**
  
      private Filter ssoFilter() {
          CompositeFilter filter = new CompositeFilter();
          List&lt;Filter&gt; filters = new ArrayList&lt;&gt;();
          filters.add(ssoFilter(facebook(), "/login/facebook"));
          filters.add(ssoFilter(github(), "/login/github"));
          **filters.add(ssoFilter(gmail(), "/login/gmail"));**
          filter.setFilters(filters);
          return filter;
      }
  
      private Filter ssoFilter(ClientResources client, String path) {
          OAuth2ClientAuthenticationProcessingFilter oAuth2ClientAuthenticationFilter = new OAuth2ClientAuthenticationProcessingFilter(
                  path);
          OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(client.getClient(), oauth2ClientContext);
          oAuth2ClientAuthenticationFilter.setRestTemplate(oAuth2RestTemplate);
          UserInfoTokenServices tokenServices = new UserInfoTokenServices(client.getResource().getUserInfoUri(),
                  client.getClient().getClientId());
          tokenServices.setRestTemplate(oAuth2RestTemplate);
          oAuth2ClientAuthenticationFilter.setTokenServices(tokenServices);
          return oAuth2ClientAuthenticationFilter;
      }
  
  }
  
  class ClientResources {
      private OAuth2ProtectedResourceDetails client = new AuthorizationCodeResourceDetails();
      private ResourceServerProperties resource = new ResourceServerProperties();
  
      public OAuth2ProtectedResourceDetails getClient() {
          return client;
      }
  
      public ResourceServerProperties getResource() {
          return resource;
      }
  }
  </code></pre>
  
  <p><strong>index.html</strong></p>
  
  <pre><code>&lt;div&gt;
      With Facebook: &lt;a href="/login/facebook"&gt;click here&lt;/a&gt;
  &lt;/div&gt;
  &lt;div&gt;
      With Github: &lt;a href="/login/github"&gt;click here&lt;/a&gt;
  &lt;/div&gt;
  **&lt;div&gt;
      With Gmail: &lt;a href="/login/gmail"&gt;click here&lt;/a&gt;
  &lt;/div&gt;**
  </code></pre>
  
  <p><strong>application.yml</strong>  // skipped the contents for github and fb to save space</p>
  
  <pre><code>gmail:
    client:
      client_id: 7xxxxxxxx-1spjexxxxxxxc.apps.googleusercontent.com
      scope: https://www.googleapis.com/auth/userinfo.profile
      client_secret: Xxxxxxx-I*****zx
      userAuthorizationUri: https://accounts.google.com/o/oauth2/auth
      accessTokenUri: https://accounts.google.com/o/oauth2/token
      auth_provider_x509_cert_url:https://www.googleapis.com/oauth2/v1/certs
  </code></pre>
  
  <p><strong>LOG</strong></p>
  
  <blockquote>
    <p>org.springframework.security.authentication.BadCredentialsException:
    Could not obtain user details from token  at
    org.springframework.security.oauth2.client.filter.OAuth2ClientAuthenticationProcessingFilter.attemptAuthentication(OAuth2ClientAuthenticationProcessingFilter.java:122)
    ~[spring-security-oauth2-2.0.10.RELEASE.jar:na]   at
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212)
    [spring-security-web-4.1.1.RELEASE.jar:4.1.1.RELEASE]     at
    org.springframework.web.filter.CompositeFilter$VirtualFilterChain.doFilter(CompositeFilter.java:112)
    [spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]  at
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
    [spring-security-web-4.1.1.RELEASE.jar:4.1.1.RELEASE]     at
    org.springframework.web.filter.CompositeFilter$VirtualFilterChain.doFilter(CompositeFilter.java:112)
    [spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]  at
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
    [spring-security-web-4.1.1.RELEASE.jar:4.1.1.RELEASE]     at
    org.springframework.web.filter.CompositeFilter$VirtualFilterChain.doFilter(CompositeFilter.java:112)
    [spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]  at
    org.springframework.web.filter.CompositeFilter.doFilter(CompositeFilter.java:73)
    [spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]</p>
    
    <p>**Caused by:
    org.springframework.security.oauth2.common.exceptions.InvalidTokenException:
    ya***********dCCnRbsve3
      at
    org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices.loadAuthentication(UserInfoTokenServices.java:91)
    ~[spring-boot-autoconfigure-1.4.0.RELEASE.jar:1.4.0.RELEASE]  at
    org.springframework.security.oauth2.client.filter.OAuth2ClientAuthenticationProcessingFilter.attemptAuthentication(OAuth2ClientAuthenticationProcessingFilter.java:112)
    ~[spring-security-oauth2-2.0.10.RELEASE.jar:na]   ... 66 common frames
    omitted</p>
  </blockquote>
  
  <p>And on Google API Console.</p>
  
  <p>Redirect URL I have given as : <strong>localhost:8080/login/gmail</strong></p>