2

The application we are building is both an iOS and Android mobile app that pairs high school students with nonprofits to help manage their Facebook pages. We assign each student to one nonprofit, make them an administrator of their Facebook page, and allow them to create posts on behalf of the organization to reach new audiences. Each student is then given a score that comprises of the likes, comments, shares, etc. garnered by the student's posts on the page.

The trouble we are having is with the Facebook Graph API. We need someone familiar with the current Facebook SDK that can help us devise a better plan to retrieve specific data.

Currently, we have made a profile that is an admin for every nonprofit Facebook page on our platform. We use this admin access to retrieve a user access token and a page access token for each Facebook page. But with the new SDK, we can only make page access tokens last 60 days, making us constantly have to refresh the token to retrieve the data. However, when we refresh, Facebook asks us to authenticate with a username and password which slows down our backend with high a risk of our server crashing. Also, this method increases the amount of daily requests we make to Facebook for data, running the risk that Facebook might shut down our access completely as we add more users to the app.

So our problem really lies with this page access token because we are trying to query data from multiple different Facebook pages. The data we are requesting is also very specific because we only want the data from posts published from a specific "admin_creator" on the page (aka the student only, not any other admin) to assign the student a score.

Is there a way to make page access tokens never expire with the current Facebook SDK? There are posts that describe this process, but they were for 2014, not the current version. Also, is there a better way than our current method to retrieve the specific data we need?

Sahil Mittal
  • 20,351
  • 12
  • 59
  • 88
Rahul Chabria
  • 37
  • 1
  • 3
  • Get a user access token, extend the user access token, call /me/accounts and the page tokens will be valid forever – WizKid Jan 20 '16 at 05:03
  • See my answer at http://stackoverflow.com/questions/23907802/post-to-a-facebook-page-without-manage-pages-permission-using-php/23908207 Congrats to your app idea, I think it's great! – Tobi Jan 20 '16 at 07:47
  • Since generating a long-lived token requires your app secret, you should not perform this action from client-side code, but do it on a server instead. – CBroe Jan 20 '16 at 08:55
  • So with the current Facebook SDK, what is the exact code I would need to use to make the page access token never expire? The problem we run into is that the token only lasts 60 days when we try to extend it – Rahul Chabria Jan 20 '16 at 14:40
  • I've explained the same but in detail what these guys have been explaining. Hope that'l clear your doubts. – Sahil Mittal Jan 21 '16 at 05:09

1 Answers1

2

Steps:

  1. Get the user-access-token having the permission manage_pages with it.
  2. Extend the token using (do it server side, since it involves app secret)

    https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id={your_app_id}&client_secret={your_app_secret}&fb_exchange_token={user_token_from_last_step}

  3. Use the extended token from above step and use the API-

    /v2.5/{page-id}?fields=access_token
    

    You'll get the page access token in response, that'll never expire.

I think you're at step 2, just follow the step 3 and you're good to go.

I'll recommend you go through this page about the access tokens and things will be much clearer.

Sahil Mittal
  • 20,351
  • 12
  • 59
  • 88
  • thanks for your response. We tried using ur steps and I posted our code below. But we are getting a constant server error. Is our code wrong? – Rahul Chabria Jan 22 '16 at 14:54
  • This answer didn't work for me, but this one did: http://stackoverflow.com/a/28418469/470749 – Ryan Jan 12 '17 at 01:39