Question

I'm trying to retrieve the app access token of my app in order to post notifications, but for some reason, it doesn't work. Here's the code:

$AppParams = array(
    'client_id'     => 'myclientid',
    '&client_secret' => 'myclientsecret',
    '&grant_type'    =>'client_credentials'
    );
$AppToken = $facebook->api('oauth/access_token?', 'GET', $AppParams);

I also replaced the first part with the full oauth/accesstoken link, but then it returns me general information about oauth and their facebook page, which I do not want. I did nearly the same thing in C# and there it works.

Was it helpful?

Solution

You don't really have to request an application access token. You can simply assemble one yourself.

An application access token is formatted like this:

app_id|app_secret

That's the application's id, a pipe character | and the application secret.

Make sure that this app token is not accessible to your users! Only use and reference it on the serverside - never pass this to the client. For more info, check out the last few sentences in the relevant documentation.

OTHER TIPS

With version 5 of the SDK you can get the access token with the accessToken() method of a FacebookApp instance. If you have a Facebook instance (as you normally would) you can get it like this:

$fb->getApp()->getAccessToken()

When I want to use my own app's access token I'm instantiating the API like this. I don't know if there's a cleaner way.

$fb = new \Facebook\Facebook([
    'default_graph_version' => 'v2.8',
]);
$fb->setDefaultAccessToken($fb->getApp()->getAccessToken());

Replace -

'&client_secret' => 'client_secret'

'&grant_type' => 'grant_type'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top