Páginas

2011-09-20

[drops.log]: Google+ API roadmap (and shortcuts...)


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:
That is it. Enjoy the Google+ small set of accessible data. And let’s hope we get more soon.

[+/-] show/hide

[drops.log]:More, more, more, plus... Google+

Not long ago I dropped something about the Google+ API release. Well, they do not let any grumpy comments (like mine) get cold. They just released the Google+ Hangout API. Well, of course I need to take a look, make some tests, but I have to much to do right now (Anyone could help with Facebook Real-time Updates on app hosted in Google Appengine?).

They announced a lot of Google+ new features today 1, 2. What do I comment about all that? Google+ really wants to get in your pants pocket (Yeah, lame joke, I know, but I don’t speak English natively), purse, bag, anywhere you keep your smart device. I guess since Facebook is getting closer of Google+ best features it seems Google wants to attract the users by that thing they are always toughing, playing, even doing nasty things: their mobiles (What? Have you known about any nude pictures taken by mobile devices and spread over the internet? Ever heard of sexting? Other bad joke... I know, but sexting is nasty, isn't it? :$).
Jokes apart I guess Google is showing their intelligence and fighting in a place they can do great: smart phones. After all they have the droids in their side... (Enough of silly jokes, bye.)

[+/-] show/hide