0

I have been developing Spring Social Google access using Spring Boot. When I am configuring

application.properties

I am unable to find any metadata of google in spring social.
Like there is metadata for facebook, twitter and linkedin.
Then How will I configure this with Client-ID and Client-Secret, which is required by connectcontroller for connecting to google.

Jarvis0809
  • 11
  • 1
  • 5

2 Answers2

2

Very recent versions with built in spring boot support look for appSecret and appId keys in application.properties under spring.social.google.

spring.social.google.appId=
spring.social.google.appSecret=

See the props file

Older code should initialize it manually in a SocialConfiguration class similar to this. Note we can use whatever keys we want in application.properties in this case.

@Configuration
@EnableSocial
public class SocialConfiguration  implements SocialConfigurer {

    private static final Logger log = LoggerFactory.getLogger(SocialConfiguration.class);

    @Autowired
    private DataSource dataSource;

    /**
     * Configures the connection factories for Facebook.
     */
    @Override
    public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
        log.debug("init connection factories");

        cfConfig.addConnectionFactory(new FacebookConnectionFactory(
                env.getProperty("facebook.app.id"),
                env.getProperty("facebook.app.secret")
        ));

        GoogleConnectionFactory googleConnectionFactory = new GoogleConnectionFactory(
                env.getProperty("google.consumerKey"),
                env.getProperty("google.consumerSecret")
        );
        googleConnectionFactory.setScope("email profile drive"); //drive.readonly drive.metadata.readonly

        cfConfig.addConnectionFactory(googleConnectionFactory);
    }

    /**
     * The UserIdSource determines the account ID of the user.
     * The demo application uses the username as the account ID.
     * (Maybe change this?)
     */
    @Override
    public UserIdSource getUserIdSource() {
        return new AuthenticationNameUserIdSource();
    }

    @Override
    public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
        return new JdbcUsersConnectionRepository(
                dataSource,
                connectionFactoryLocator,
                /**
                 * The TextEncryptor encrypts the authorization details of the connection.
                 * Here, the authorization details are stored as PLAINTEXT.
                 */
            Encryptors.noOpText()
        );
    }

    /**
     * This bean manages the connection flow between
     * the account provider and the example application.
     */
    @Bean
    public ConnectController connectController(final ConnectionFactoryLocator connectionFactoryLocator,
                                               final ConnectionRepository connectionRepository) {

        return new ConnectController(connectionFactoryLocator, connectionRepository);
    }

    @Bean
    @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
    public Google google(final ConnectionRepository repository) {
        final Connection<Google> connection = repository.findPrimaryConnection(Google.class);
        if (connection == null)
            log.debug("Google connection is null");
        else
            log.debug("google connected");
        return connection != null ? connection.getApi() : null;
    }

    @Bean(name = { "connect/googleConnect", "connect/googleConnected" })
    @ConditionalOnProperty(prefix = "spring.social", name = "auto-connection-views")
    public GenericConnectionStatusView googleConnectView() {
        return new GenericConnectionStatusView("google", "Google");
    }

}
Lucas Holt
  • 3,826
  • 1
  • 32
  • 41
2

Spring social doesn’t have autoconfiguration done for Google. So adding any new properties wont work. You need to find a class (Ctrl+Shift+T in eclipse) and look for FacebookAutoConfiguration class you should be able to find it in org.springframework.boot.autoconfigure.social package in spring-autoconfigure.jar Copy this File and replace Facebook with Google.

Alternatively, copy the below class and put in some package, make sure it is available in classpath(src/main/java or inside another source folder)

Follow this Link for more details

@Configuration
@ConditionalOnClass({ SocialConfigurerAdapter.class, GoogleConnectionFactory.class })
@ConditionalOnProperty(prefix = "spring.social.google", name = "app-id")
@AutoConfigureBefore(SocialWebAutoConfiguration.class)
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
public class GoogleAutoConfiguration {

    @Configuration
    @EnableSocial
    @EnableConfigurationProperties(GoogleProperties.class)
    @ConditionalOnWebApplication
    protected static class GoogleConfigurerAdapter extends SocialAutoConfigurerAdapter {

        private final GoogleProperties properties;

        protected GoogleConfigurerAdapter(GoogleProperties properties) {
            this.properties = properties;
        }

        @Bean
        @ConditionalOnMissingBean(Google.class)
        @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
        public Google google(ConnectionRepository repository) {
            Connection connection = repository.findPrimaryConnection(Google.class);
            return connection != null ? connection.getApi() : null;
        }

        @Bean(name = { "connect/googleConnect", "connect/googleConnected" })
        @ConditionalOnProperty(prefix = "spring.social", name = "auto-connection-views")
        public GenericConnectionStatusView googleConnectView() {
            return new GenericConnectionStatusView("google", "Google");
        }

        @Override
        protected ConnectionFactory<?> createConnectionFactory() {
            return new GoogleConnectionFactory(this.properties.getAppId(), this.properties.getAppSecret());
        }

    }

}

Now Add GoogleProperties

In the same package add the below class

import org.springframework.boot.autoconfigure.social.SocialProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "spring.social.google")

public class GoogleProperties extends SocialProperties{


}

For more explanation and step by step guide follow this link

Abhishek Galoda
  • 2,753
  • 24
  • 38
  • A+ to you man! Worked just fine to me, except that I had to remove the @EnableSocial, because it was giving me a preparedstatement error. – viniciusalvess Jan 29 '18 at 20:05