Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Facebook Graph API without login

I've seen that with Javascript API and via GET requests, it is possible to get public posts from FanPages. What i'm trying to acomplish is an Android Native App that doesn't ask user to get logged and with the FB App access token gets and shows the posts from the FB Page. I'm wondering if this is possible via Android FB API as I can accomplish this via urls like

https://graph.facebook.com/{page_id}/feed?fields=message,picture&limit=10&access_token={your_acces_token}

When i'm trying this with Android Graph API, without user loging,

AccessToken.getCurrentAccessToken()

returns null

Thank you

like image 789
PereCullera Avatar asked Nov 04 '15 21:11

PereCullera


People also ask

How do I get my Facebook graph API User ID?

The simplest way to get a copy of the User Profile object is to access the /me endpoint: FB. api('/me', function(response) { }); This will this will return the users name and an ID by default.

Is Facebook graph API rest?

Yes, it is a REST API as well.


1 Answers

I was struggling with the same for a while and after hours of research I came up with this (source):

@Override
protected void onCreate(Bundle savedInstanceState) {

  FacebookSdk.sdkInitialize(getApplicationContext());
  AppEventsLogger.activateApp(getApplication());

  AccessToken at         = new AccessToken(getString(R.string.facebook_access_token), getString(R.string.facebook_app_id), getString(R.string.facebook_page_id), null, null, null, null, null);
  Bundle      parameters = new Bundle();
  parameters.putString("fields", "message,created_time,link,full_picture");
  new GraphRequest(at, "/" + at.getUserId() + "/feed", parameters, HttpMethod.GET, new GraphRequest.Callback() {

    public void onCompleted(GraphResponse response) {

      Log.i(this.getClass().getName(), "onCompleted " + response.getRawResponse());
    }
  }).executeAsync();
}

I assume you already have a page access token. I created mine with the graph explorer from facebook. Here is a tutorial on how to create a never expiring one: How to get a never expiring Facebook Page Access Token (look for the 2016 Method)

As far as I know, the extended token does not expire as long as it is used. But I am not sure about that. I think I red this somewhere in the facebook api docs but cannot find it anymore.

There are a lot other parameters you can set. See here Page and look for 'Fields'

Hope this clumsy solution is helpfull to someone.

like image 191
aProgger Avatar answered Oct 02 '22 02:10

aProgger