I want my app to just display the posts published by a facebook page without any authentication. Here is the code:
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
String APP_ID = "1234567890";
String APP_SECRET = "1a2b3c4defghijk";
String OWNER_OF_FEED = "somepage";
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(
"https://graph.facebook.com/oauth/access_token?client_id="+
APP_ID +
"&client_secret="+APP_SECRET+"&grant_type=client_credentials");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String access_token = client.execute(get, responseHandler);
String uri = "https://graph.facebook.com/" + OWNER_OF_FEED + "/feed?"
+ access_token;
get = new HttpGet(uri);
String responseBody = client.execute(get, responseHandler);
// responseBody contains JSON-encoded feed
//textview.setText(responseBody);
TextView txtv = (TextView) findViewById(R.id.feed);
txtv.setText(String.valueOf(responseBody));
}
catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
But am unable to get any content on the app. I have included the permission for internet access in the manifest file.
New code:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
URL url;
try {
url = new URL("https://www.facebook.com/feeds/page.php?format=rss20&id=289781571139242");
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
int data = isw.read();
while (data != -1) {
char current = (char) data;
data = isw.read();
System.out.print(current);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
There is actually a way to get a page public feed without user permission!
Solution
Get the page Facebook Profile Id: the simplest way is to query the Graph API http://graph.facebook.com/{YourPageUsername} . e.g. for oStream Android App https://graph.facebook.com/oStreamApp will return Facebook Profile Id 289781571139242
Request the RSS of the Facebook page feed: Make an http request to https://www.facebook.com/feeds/page.php?format=rss20&id={yourAppId} e.g. For oStream Android App the Public https://www.facebook.com/feeds/page.php?format=rss20&id=289781571139242
An Android implementation will simply be:
Side note This question is not very much about Android but just with Facebook API.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With