简体   繁体   中英

Get an access token without being connected to facebook

I need to retrieve a facebook page's list of posts (feed) using their javascript SDK, just like they explain in their docs: https://developers.facebook.com/docs/graph-api/reference/v2.4/page/feed

/* make the API call */
FB.api(
    "/{page-id}/posts",
    function (response) {
      if (response && !response.error) {
        /* handle the result */
      }
    }
);

I need it to be my website's "news section", so users should see it even if they are not connected to facebook.

The problem

Cool, but there is a problem... It returns: An access token is required to request this resource. Holy cow... I'd like to get some access token for you @facebook, but my app doesn't make use of your authentication tools/plugins. ANYWAY, I tried with FB.getLoginStatus(); but doesn't work, because the only way it can return an access_token is if the user is actually connected to the application. My users may not even be logged to facebook !

So, ¿How can I get an access_token to be stored into a variable, and later be used to get /{my-page}/posts ?

I've already payed a look to this SO question , but it doesn't solves my problem, simply because there are no such "generic tokens". I've also read https://developers.facebook.com/docs/facebook-login/access-tokens/ and that also relies on tokens generated through facebook login methods... So, can't I display a list of fb page's posts in my website, without being connected into facebook, hence an application?

ADD: My app is build with angularJS, I'm not dealing with server-side code. I shall rely purely on javascript methods.

You could either use an page or an app access token, but as you'd be using them on the client-side, neither of them are an option.

See

Note that because this request uses your app secret, it must never be made in client-side code or in an app binary that could be decompiled. It is important that your app secret is never shared with anyone. Therefore, this API call should only be made using server-side code.

I'd strongly recommend to build a simple server-side script (PHP for example) to proxy the request to the Graph API. You could then call this via AJAX for example and load the posts asynchronously (and alse get rid of the FB JS SDK!). There is NO way to handle this in a secure manner if you don't want to use FB Login for all of your users (which also doesn't make much sense IMHO).

I think it's straightforward :)
Since pages' posts are always public , it only needs a valid access token to retrieve page posts. Quoting what you've written:

So, ¿How can I get an access_token to be stored into a variable, and later be used to get /{my-page}/posts?

You only require an access token.
My suggestion would be;
- Generate an access token for yourself (no extra permission needed) - request page-id/posts

This way you don't require other users to be connected to facebook, you can simply requests page-id/posts to retrieve posts with access token you generated for yourself.

I hope it solves your problem :D
TIP : As long as posts are public , you only require a valid access token, it doesn't need to be user or page specific.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM