Despite the commotion about Jobs’s death, many probably don’t know about the death of Dennis Ritchie (even that being in second on Twitter Worldwide Trend Topics while I was writing this post). Yeah, who the hell is Dennis Ritchie? I had no idea either, but his legacy is much bigger than Jobs’s.
2011-10-13
Fuck off death reaper! Dennis Ritchie too?!
2011-10-05
[drops.log]: Life follows...
I was thinking in to entitle this post as “Apple loses jobs even no one was fired”. But I guess the main thing here is: life follows.Steve Jobs passed away but that means little. Better products weren’t going to stop of show up. Apple is not going to die tomorrow.
2011-09-30
[drops.log]: An inverse path, from photo to album
Wow! I’ve been absent since last week. My logs didn’t increase much in the while. I keep groping the Facebook Graph API with the restFB java wrapper.
//posts is a Post list retrived from a user's feed Post post = posts.get(i); if(post.getType() != null && post.getType().equals("photo")) { //Got a photo if(post.getCaption() != null) { //it is from a new album //this regex compilation could be static final String ALBUM_REGEX = "(?<=set=a\\.)\\d+"; Pattern pattern = Pattern.compile(ALBUM_REGEX); Matcher matcher = pattern.matcher(post.getLink()); if(matcher.find()) { String albumId = matcher.group(); Connection< Photo > photos = fbcClient.fetchConnection(albumId + "/photos", Photo.class); for(Photo photo : photos.getData()) { //do your job with the photos } } } }
2011-09-24
[drops.log]: To pay attention usually pays the effort
That title is a bit vague but can get handy. I commented I got stuck with Facebook Real-time Updates API. The problem was incompatibility between my subscription and user authorization. I subscribed to users feed, but never asked them (myself in the case) for the auth to read their feeds. Of course facebook would never pass me content users never allowed me to get even with a offline access token. By the way, I haven't tried yet, but I am pretty sure you will need one to handle updates processing since the updates are mere announces, no content is delivered.
2011-09-23
[drops.log]: Facebook in Java
I started to do a small piece of code to interact with facebook. I spent some days wondering why the hell my application wasn’t working. The case is bad documentation on Facebook developers page. They say you should pass the authorization code and your app secret to the Graph API token endpoint from an URL like this:
public class FacebookAuthorizationFilter implements Filter { public static final String AUTH_URL_BASE = "https://www.facebook.com/dialog/oauth?client_id=%s&redirect_uri=%s&scope=%s"; public static final String ACCESS_TOKEN_URL_BASE = "https://graph.facebook.com/oauth/access_token?client_id=%s&redirect_uri=%s&client_secret=%s&code=%s"; @Override public void init(FilterConfig filterConfig) throws ServletException { /*init code*/ } @Override public void destroy() { /*destroy code*/ } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse resp = (HttpServletResponse) response; String code = request.getParameter(MyConstants.CODE_PARAM_KEY); String accessToken = request.getParameter(MyConstants.ACCESS_TOKEN_PARAM_KEY); if(accessToken != null) { //there is one already let the application follow usually chain.doFilter(req, resp); return; }else { if(code == null) { //ask for authorization from facebook resp.sendRedirect(String.format(AUTH_URL_BASE, this.appKey, this.redirectURI, this.scope)); } else { //already authorized, fetch the access_code accessToken = retriveAccessToken(code); //save the access_token req.getSession().setAttribute(MyConstants.ACCESS_TOKEN_KEY, accessToken); chain.doFilter(req, resp); } } } /** * Retrieves the content of the page with the access_code token * @param code the code parameter passed to the redirect_uri from the oath/dialog page * @return The String access_token provided from facebook * @throws IOException */ private String retriveAccessToken(String code) throws IOException { URL url = new URL(String.format(ACCESS_TOKEN_URL_BASE, this.appKey, this.redirectURI, this.appSecret, code)); //TODO: Try to sintetize with CharStreams from guava-libraries ByteArrayOutputStream byteWriter = new ByteArrayOutputStream(); InputStream urlReader = url.openStream(); int r; while((r = urlReader.read()) != -1) { byteWriter.write(r); } String fetchedResult = new String(byteWriter.toByteArray()); String[] fields = fetchedResult.split("&"); //TODO: Make a regex to extract that for(String field : fields) { String[] pair = field.split("="); if(pair.length == 2 && pair[0].equals("access_token")) { return pair[1]; } } throw new RuntimeException(fetchedResult); } }
- Make a filter that asks for the client object or access_token on the session objectc
- If there is no parameter in the session stops the request and redirect to:
- Where scope are the permissions you want
- The redirect_uri is going to work this case and you must to have a filter in the redirect_uri able to get the code parameter (line 30 in the snippet above)
- Fetch the content of the follow URL:
- (Lines 48 to 56 in the snippet above)
- Parse the content to extract the parameters (at least acess_token) (lines 60 to 65 on the snippet above)
- With access_token you can create the actual client object to access facebook data (line 37 on the snippet above)
- Put the object where you can retrieve it (line 35 in the snippet above)
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.
- 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