Skip to main content

How to access google data using spring-social and spring boot

How to access google data using spring-social

Introduction

Spring social do have a spring-social-google project but it doesn’t have autoconfiguration for Google. So google authorization doesn’t work with spring boot autoconfigure straight away. I had created a pull request and merged the code to spring repository but got the following reply
Thanks for the PR. We’ve already considered this in #2548 where we decided that it isn’t something that we want to include in Spring Boot. That’s still the case so I’m going to close this. Thanks anyway.

So it doesn’t look like sooner or later they are going to add autoconfigure functionality to Spring-Social-Google

What needs to be done to enable spring social autoconfigure

    To enable autoconfigure of spring-social-google we need to
  • Add Maven dependency
  • Add a GoogleAutoConfiguration
  • Add GoogleProperties
  • Modify HTML pages

Getting started

I would suggest you to first clone the Access Facebook Data Tutorial as it does have required page structure and dependencies

Add Maven Dependencies

Add the spring-social-google dependency

Add a GoogleAutoConfiguration Class

Do Ctrl+Shift+T  in your IDE(eclipse) and look for FacebookAutoConfiguration class you should be able to find it either 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)

Add GoogleProperties

In the same package add the below class

Update the application.properties

Now update the application.properties with your secret and client key

Add the HTML for Google

Under src/main/resources/templates/connect you should have facebookConnect.html and facebookConnected.html, if you have cloned the project properly.Again replace facebook with google in HTML and save them as googleConnected.html and googleConnect.html or copy them as shown below.

googleConnect.html

googleConnected.html

Add a @Controller for Google

We need to add a rest controller so it can handle google authorization requests

@RequestMapping(“/google”), your application should run on localhost:port/google to test this example

Add HTML to show user details

In src/main/resources add a file called “google.html” at same level as “hello.html”

That’s all

Run the application on localhost: port/google and you should be authenticated. Drop comments if you face any problem

Please note that this example works only for one user, if you want to make it work for multiple users go through below link

Happy Coding, hope it helps.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami