Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the App Access Token via the Facebook PHP SDK?

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.

like image 722
Boehmi Avatar asked Sep 10 '13 09:09

Boehmi


3 Answers

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.

like image 131
Lix Avatar answered Oct 19 '22 15:10

Lix


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());
like image 36
tremby Avatar answered Oct 19 '22 15:10

tremby


Replace -

'&client_secret' => 'client_secret'

'&grant_type' => 'grant_type'
like image 21
Sahil Mittal Avatar answered Oct 19 '22 14:10

Sahil Mittal