I was browsing Facebook's documentation reading about canvas applications and I came across an example application: http://developers.facebook.com/docs/samples/canvas. As I read through their example, however, I got very confused about their use of cookies in the iframe application.
A little backstory...
I had already played around with using iframes for embeddable widgets (unrelated to Facebook) and I found out a few browsers (Chrome, Safari, etc.) have strict cookie policies and don't allow cross-domain cookies set in iframes (Firefox, on the other hand, allows iframes to set cross-domain cookies in iframes). For example, if foo.com has an iframe with src="http://bar.com/widget" the iframe widget will not be able to set any cookies for bar.com and therefore will have trouble persisting state within the iframe: bar.com will interpret every request (including ajax requests) from the widget as a fresh request without an established session. I struggled, and found a way around this by using JSONP and javascript to set cookies for foo.com instead...
... and so?
Well, I was looking at the example canvas iframe Facebook application and I noticed that their application (hosted on runwithfriends.appspot.com) is able to set a cookie, u, with the current user's id along with a few other parameters for the runwithfriends.appspot.com domain. It sends this cookie with every request... and it works in both Chrome and Firefox! WTF? How does Facebook get around the cross-domain cookie restrictions on Chrome?
(I already know the answer now, but I thought this might be helpful for anyone struggling to figure out the same thing)
So the iFrame isn't actually setting the u cookie for the runwithfriends.appspot.com domain. What Facebook does is it creates a form,
and uses javascript to submit the form on page load. Since the form's target is the iframe, it doesn't reload the page... it just loads the iframe with the POST's response. Apparently even Chrome and other browsers with strict cookie policies set cookies for cross domain requests if they are POST requests...
As you can see the answer is simple, and could be implemented by just creating a handler (could be a Servlet or anything that can catch some parameterts and transform them into cookies), like this:
Showing posts with label Facebook. Show all posts
Showing posts with label Facebook. Show all posts
Friday, October 18, 2013
Monday, October 29, 2012
Interacting with Facebook using Java
If it's your first time interacting with Facebook, probably you haven't found many examples in Java, have you?
On my case I'm working with something really simple, I just need to create a simple app that will be configured as Canvas. There are 2 important steps in order to work with Facebook by using Canvas:
1. Authorization
When Facebook calls your webpage (on my case a jhtml using SEAM - it could be a JSP as well), it will send through POST something called signed_request, for more details please read this The signed_request parameter is the concatenation of a HMAC SHA-256 signature string, a period (.), and a base64url encoded JSON object. It looks something like this (without the newlines):
However, in order to play around with JSON object, we need to decode it, and this will do the trick: It's very important to use sun.misc.BASE64Decoder in order to avoid missing few characters when decoding with other classes. Belive me, this save you from a lot of headeaches. I'm a big fan of Commons, however, it didn't work out really good when decoding this JSON object. FYI, this is my method.
2. Authentication
This step is really easy, because based on attributes oauth_token and expires, we have a to make a http call to https://graph.facebook.com/me in order to authenticate into FB.
As result of my http call, by using the JSON object returned, I'm able to get more details about this FB account, such as email, first name, last name, age, languages, among others. The next steps depend how deep you want to go.
Good luck!
On my case I'm working with something really simple, I just need to create a simple app that will be configured as Canvas. There are 2 important steps in order to work with Facebook by using Canvas:
1. Authorization
When Facebook calls your webpage (on my case a jhtml using SEAM - it could be a JSP as well), it will send through POST something called signed_request, for more details please read this The signed_request parameter is the concatenation of a HMAC SHA-256 signature string, a period (.), and a base64url encoded JSON object. It looks something like this (without the newlines):
vlXgu64BQGFSQrY0ZcJBZASMvYvTHu9GQ0YM9rjPSso . eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsIjAiOiJwYXlsb2FkIn0
Just by receiving this parameter, it means that you (as facebook user) just authorized this app on your account, otherwise you'll get the classic mesage asking for your permission to use this app. Next, we need to retrieve from the JSON object 2 parameters that we need to do the authentication: oauth_token and expires).
However, in order to play around with JSON object, we need to decode it, and this will do the trick: It's very important to use sun.misc.BASE64Decoder in order to avoid missing few characters when decoding with other classes. Belive me, this save you from a lot of headeaches. I'm a big fan of Commons, however, it didn't work out really good when decoding this JSON object. FYI, this is my method.
2. Authentication
This step is really easy, because based on attributes oauth_token and expires, we have a to make a http call to https://graph.facebook.com/me in order to authenticate into FB.
As result of my http call, by using the JSON object returned, I'm able to get more details about this FB account, such as email, first name, last name, age, languages, among others. The next steps depend how deep you want to go.
Good luck!
Subscribe to:
Posts (Atom)