Well, my foreplay with Google+ API, launched past week, was too rush and to practical. Let’s make some notes useful for the next time.
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
There are start sample code provided for some languages wrappers, the one for JAVA is quite good. It shows for instance how get the access token. Let’s make a Servlet Filter (analogous to drps.log about authentication of FB apps that I showed) to do the job of checking or authorising, retrieving access token... It is very simple, but a bit verbose:
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:
- Google API Console: https://code.google.com/apis/console
- Google Client API Downloads Page: http://developers.google.com/+/downloads
- Google API Java Client Developer’s Page (be aware they misleading concerning the versions of Client API wrapper and Service API wrappers, use 1.5.0-beta for the first one and 1.2.1 for the Google+ Service API wrapper): http://code.google.com/p/google-api-java-client/wiki/DeveloperGuide
- Google+ Developers Discussion Group: http://groups.google.com/group/google-plus-developers
- Google Client API Javadoc: http://javadoc.google-api-java-client.googlecode.com/hg/1.5.0-beta/index.html
- Google+ API Javadoc: http://javadoc.google-api-java-client.googlecode.com/hg/apis/plus/v1/index.html
- Bugs, issues and Features requests of Google+ platform: http://code.google.com/p/google-plus-platform/issues/list
- Bugs, issues and Features requests for the JAVA wrappers: http://code.google.com/p/google-api-java-client/issues
- Authorized Access Sites, Apps and Services configuration page: https://accounts.google.com/b/0/IssuedAuthSubTokens
That is it. Enjoy the Google+ small set of accessible data. And let’s hope we get more soon.