First observations:
- To work with Google+ API we need the Google Client API to get access to user google account and the Google Plus Service API. Make sure to download both
- We need to get permission from our apps access Google services and data. SO you need to sign in Google API Console
- Create the app by creating a Client ID, providing type of app, hostname address
- Switch to ON the status of the services you will need to access from our app
One note for JAVA users, when I was writing this post the documentation was a bit misleading. The feature version of the JAVA wrapper for the Client API was 1.4.3 but version of the google plus API in the Developer’s Guide page was 1.2.1 which is incompatible with the 1.4.3 version, so download the 1.5.0 version
public class GoogleAuthFilter implements Filter {
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
AccessTokenResponse accessToken = (AccessTokenResponse)req.getSession().getAttribute(MyConstants.ACCESS_TOKEN_RESPONCE_KEY);
//test to check access credential
if(accessToken == null) {
String code = req.getParameter(MyConstants.CODE_KEY);
if(code == null) {
//no code means the user must allow the app access their data
AuthorizationRequestUrl authUrl = new GoogleAuthorizationRequestUrl(
MyConstants.CLIENT_ID,
MyConstants.REDIRECT_URI,
MyConstants.GOOGLE_PLUS_SCOPE_URL);
resp.sendRedirect(authUrl.build());
} else {
//use the code to generate access token
accessToken = new GoogleAccessTokenRequest.
GoogleAuthorizationCodeGrant(
new NetHttpTransport(),
new GsonFactory(),
MyConstants.CLIENT_ID,
MyConstants.CLIENT_SECRET,
code,
MyConstants.REDIRECT_URI
).execute();
req.getSession().setAttribute(MyConstants.ACCESS_TOKEN_RESPONCE_KEY, accessToken);
resp.sendRedirect(MyConstants.SERVLET_PATH);
}
}
chain.doFilter(req, resp);
}
}
To get the Plus service client object is also simple and verbose, just snip of a servlet to exemplify:
AccessTokenResponse accessToken = (AccessTokenResponse)req.getSession().getAttribute(MyConstants.ACCESS_TOKEN_RESPONCE_KEY);
GoogleAccessProtectedResource requestInitializer = new GoogleAccessProtectedResource(
accessToken.accessToken,
new NetHttpTransport(),
new GsonFactory(),
MyConstants.CLIENT_ID,
MyConstants.CLIENT_SECRET,
accessToken.refreshToken);
Plus plus = new Plus(new NetHttpTransport(), requestInitializer, new GsonFactory());
With the Plus object you can make requests to access people data
String who = req.getParameter(MyConstants.WHO_KEY);
who = who == null ? "me" : who;
Person me = plus.people.get(who).execute();
You could access the activities too, but just the public.
We are almost finishing. Let’s list (or relist) useful links too:
That is it. Enjoy the Google+ small set of accessible data. And let’s hope we get more soon.
[+/-] show/hide
[+/-] show/hide