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.
- 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
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);
}
}
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());
String who = req.getParameter(MyConstants.WHO_KEY); who = who == null ? "me" : who; Person me = plus.people.get(who).execute();
- 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