Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get a facebook page's feed onto my app

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;
    }

}
like image 913
krtkush Avatar asked Nov 29 '22 01:11

krtkush


1 Answers

There is actually a way to get a page public feed without user permission!

Solution

  1. 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

  2. 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:

  1. Make an http request to http://graph.facebook.com/{YourPageUsername}
  2. Parse the content and get the YourPageId
  3. Make an http request to https://www.facebook.com/feeds/page.php?format=rss20&id={yourAppId}
  4. Parse the content
  5. Display the content as you wish

Side note This question is not very much about Android but just with Facebook API.

like image 86
Stefano Pochet Avatar answered Dec 04 '22 21:12

Stefano Pochet