Saturday, February 26, 2011

Facebook API from Java code

I just got started with Facebook (graph)  API and fetch data from native Java programs for my project requirements .First of all I created an sample app at http://developers.facebook.com/ and got the necessary details about the app at the summery.



Once I registered the app it was time to give permission from my user account to the app so that it can access ( so the API ) data from my account.
https://www.facebook.com/dialog/oauth?
     client_id=MY_APP_ID&redirect_uri=MY_URL

MY_URL was any random url as the landing page after I log in was immaterial in this case as I was going to use the APIs from the java code.
https://www.facebook.com/dialog/oauth?
     client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=email,read_str,offline_access

I addded offline_access so that it does not time out , I can use the access token generated forever.
Now after I allowed access it landed on a page similar to the following
http://YOUR_URL?code=A_CODE_GENERATED_BY_SERVER

To get the access_token I invoked following
https://graph.facebook.com/oauth/access_token?
client_id=<MY_APP_ID>&redirect_uri=YOUR_URL&
client_secret=YOUR_APP_SECRET&code=THE_CODE_FROM_ABOVE

once the access token appeared in the response I invoked various graph APIs to test if data is coming in JSON format

https://graph.facebook.com/me?access_token=<Access_token_obtained_from_last_step>
gives me all my wall posts

https://graph.facebook.com/<POST_ID>?access_token=<Access_token_obtained_from_last_step>
gives me all the comments for a given post id of mine

I also used json-simple to parse and decode the JSON returned by the above Facebook APIs.
/*FindComments.java*/

import java.io.BufferedReader;


import java.io.IOException;

import java.io.InputStreamReader;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLConnection;

import org.json.simple.JSONArray;

import org.json.simple.JSONObject;

import org.json.simple.JSONValue;

public class FindComments {
/**

* @param args

* @throws IOException

*/

public static void main(String[] args) throws IOException {
String commentID = "<COMMEN_ID>";

String accessToken = "<ACCESS_TOKEN>";
// TODO Auto-generated method stub

URL yahoo = new URL(

"https://graph.facebook.com/"+commentID+"?access_token="+accessToken);

URLConnection yc = yahoo.openConnection();

BufferedReader in = new BufferedReader(new InputStreamReader(yc

.getInputStream()));

String inputLine;

String s = "";

while ((inputLine = in.readLine()) != null)

// System.out.println(inputLine);

s = s + inputLine + "n";

in.close();
// System.out.println(s);
Object obj = JSONValue.parse(s);

// JSONArray array=(JSONArray)obj;

// System.out.println(obj);

// System.out.println(array.get(1));

JSONObject obj2 = (JSONObject) obj;
JSONObject comments = (JSONObject) obj2.get("comments");

JSONArray commentsArray = (JSONArray) comments.get("data");

// System.out.println(commentsArray.size());

// System.out.println(obj2.get("comments"));

for (int i = 0; i &lt; commentsArray.size(); i++) {

JSONObject commentobj = (JSONObject) commentsArray.get(i);

// System.out.println(commentobj);

String commentText = (String) commentobj.get("message");

System.out.println(commentText);

}
}

}

The above code prints the comments given on the Hard coded post ID

Facebook API from Java code

I just got started with Facebook (graph)  API and fetch data from native Java programs for my project requirements .First of all I created an sample app at http://developers.facebook.com/ and got the necessary details about the app at the summery.



Once I registered the app it was time to give permission from my user account to the app so that it can access ( so the API ) data from my account.
https://www.facebook.com/dialog/oauth?
     client_id=MY_APP_ID&redirect_uri=MY_URL

MY_URL was any random url as the landing page after I log in was immaterial in this case as I was going to use the APIs from the java code.
https://www.facebook.com/dialog/oauth?
     client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=email,read_str,offline_access

I addded offline_access so that it does not time out , I can use the access token generated forever.
Now after I allowed access it landed on a page similar to the following
http://YOUR_URL?code=A_CODE_GENERATED_BY_SERVER

To get the access_token I invoked following
https://graph.facebook.com/oauth/access_token?
client_id=<MY_APP_ID>&redirect_uri=YOUR_URL&
client_secret=YOUR_APP_SECRET&code=THE_CODE_FROM_ABOVE

once the access token appeared in the response I invoked various graph APIs to test if data is coming in JSON format

https://graph.facebook.com/me?access_token=<Access_token_obtained_from_last_step>
gives me all my wall posts

https://graph.facebook.com/<POST_ID>?access_token=<Access_token_obtained_from_last_step>
gives me all the comments for a given post id of mine

I also used json-simple to parse and decode the JSON returned by the above Facebook APIs.
/*FindComments.java*/

import java.io.BufferedReader;


import java.io.IOException;

import java.io.InputStreamReader;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLConnection;

import org.json.simple.JSONArray;

import org.json.simple.JSONObject;

import org.json.simple.JSONValue;

public class FindComments {
/**

* @param args

* @throws IOException

*/

public static void main(String[] args) throws IOException {
String commentID = "<COMMEN_ID>";

String accessToken = "<ACCESS_TOKEN>";
// TODO Auto-generated method stub

URL yahoo = new URL(

"https://graph.facebook.com/"+commentID+"?access_token="+accessToken);

URLConnection yc = yahoo.openConnection();

BufferedReader in = new BufferedReader(new InputStreamReader(yc

.getInputStream()));

String inputLine;

String s = "";

while ((inputLine = in.readLine()) != null)

// System.out.println(inputLine);

s = s + inputLine + "\n";

in.close();
// System.out.println(s);
Object obj = JSONValue.parse(s);

// JSONArray array=(JSONArray)obj;

// System.out.println(obj);

// System.out.println(array.get(1));

JSONObject obj2 = (JSONObject) obj;
JSONObject comments = (JSONObject) obj2.get("comments");

JSONArray commentsArray = (JSONArray) comments.get("data");

// System.out.println(commentsArray.size());

// System.out.println(obj2.get("comments"));

for (int i = 0; i &lt; commentsArray.size(); i++) {

JSONObject commentobj = (JSONObject) commentsArray.get(i);

// System.out.println(commentobj);

String commentText = (String) commentobj.get("message");

System.out.println(commentText);

}
}

}

The above code prints the comments given on the Hard coded post ID

Saturday, February 19, 2011

Veronica on Astronomy show

I just came across this funny video about science in astronomer Phil Plait's blog which features Veronica Belmont

[youtube http://www.youtube.com/watch?v=nk_80e2owNo&w=640&h=390]

Courtesy

Veronica on Astronomy show

I just came across this funny video about science in astronomer Phil Plait's blog which features Veronica Belmont


Courtesy

Friday, February 4, 2011

Automating interactive ssh commands in Linux

I ofter encountered problem while doing SSH to a remote server manually and giving the password when I am prompted for.

I found a way to automate this process or any other interactive shell operation using expect utility.This can also be used while doing FTP to a remote server when you want to automate all the repeatable operations .


install expect and autoexpect from your in your linux machine (Ubuntu)



sudo apt-get install expect

sudo apt-get install expect-dev


Issue the usual command prefixed with autoexpect to generate myexp.exp file


autoexpect -f myexp.exp ssh -l <username> <hostname>

Exit the shell immediately to stop recording and  open the file myexp.exp and remove everything after send command

send -- "<password>r"
Append  "interact" at the end so that interactive shell continues


Now issue the following command to automate the login process without being prompted for password


expect myexp.exp

one can also put it in .cshrc to set it as an alias to the login script.


Reference
http://blog.rackwire.com/2010/05/using-expect-and-autoexpect-to-automate-tasks-in-linux/

Automating interactive ssh commands in Linux

I ofter encountered problem while doing SSH to a remote server manually and giving the password when I am prompted for.

I found a way to automate this process or any other interactive shell operation using expect utility.This can also be used while doing FTP to a remote server when you want to automate all the repeatable operations .


install expect and autoexpect from your in your linux machine (Ubuntu)



sudo apt-get install expect

sudo apt-get install expect-dev


Issue the usual command prefixed with autoexpect to generate myexp.exp file


autoexpect -f myexp.exp ssh -l <username> <hostname>

Exit the shell immediately to stop recording and  open the file myexp.exp and remove everything after send command

send -- "<password>\r"
Append  "interact" at the end so that interactive shell continues


Now issue the following command to automate the login process without being prompted for password


expect myexp.exp

one can also put it in .cshrc to set it as an alias to the login script.


Reference
http://blog.rackwire.com/2010/05/using-expect-and-autoexpect-to-automate-tasks-in-linux/