Martha Smithers
Martha Smithers

Reputation: 153

Notifications API with PHP SDK

I am trying to send notification to one of my users (https://developers.facebook.com/docs/app_notifications/) but I can't manage to get this work with my PHP SDK.

I am using this code:

$data = array(
    'href'=> 'https://apps.facebook.com/MY_APP/',
    'access_token'=> $app_token,
    'template'=> 'test'
);
$sendnotification = $facebook->api('/USER_ID/notifications', 'post', $data);

And this is what error I get back:

Fatal error: Uncaught OAuthException: Invalid OAuth access token signature. thrown in /usr/home/test/base_facebook.php on line 1039

This is how I get app access token:

$APPLICATION_ID = "MY_APP_ID";
$APPLICATION_SECRET = "MY_APP_SECRET";

$token_url =    "https://graph.facebook.com/oauth/access_token?" .
                "client_id=" . $APPLICATION_ID .
                "&client_secret=" . $APPLICATION_SECRET .
                "&grant_type=client_credentials";
$app_token = file_get_contents($token_url);

What am I doing wrong?


There was a problem with access_token, if I use access token directly in sode it now works.

'access_token'=> 'K3Rds2y0cGm...',

Does anyone knows how long app access token lasts?

Upvotes: 1

Views: 10776

Answers (5)

Tatarasanu Victor
Tatarasanu Victor

Reputation: 654

$accessToken = $appId . '|' . $secret;
$params = array(
            'access_token' => $accessToken,
            'href' => $canvasPage,
            'template' => $message,
        );
$facebookObject->api('/' . $userId . '/notifications/', 'post', $params);

This works for me. Reference: http://calibrate.be/labs/facebook-notification-api-php-sdk

Upvotes: 3

Dan Stern
Dan Stern

Reputation: 2167

the poblem is that the var $app_token is a string in the form of $app_token = "access_token=YOUR_APP_ACCESS_TOKEN"

what you need to use is the second part, YOUR_APP_ACCESS_TOKEN, therefore, before using $app_token as a valid token, you must delete the first part of the string "access_token="

solution:

$APPLICATION_ID = "MY_APP_ID";
$APPLICATION_SECRET = "MY_APP_SECRET";

$token_url =    "https://graph.facebook.com/oauth/access_token?" .
                "client_id=" . $APPLICATION_ID .
                "&client_secret=" . $APPLICATION_SECRET .
                "&grant_type=client_credentials";
$app_token = file_get_contents($token_url);
$app_token = str_replace("access_token=", "", $app_token);

$data = array(
    'href'=> 'https://apps.facebook.com/MY_APP/',
    'access_token'=> $app_token,
    'template'=> 'test'
);
$sendnotification = $facebook->api('/USER_ID/notifications', 'post', $data);

hope it helps

Upvotes: 4

Rolf
Rolf

Reputation: 381

This is the correct way to set an Application Access Token when you use the Facebook PHP SDK V3.2.0 :

    $this->_objFacebook->setAccessToken($this->_objFacebook->getAppId().'|'.$this->_objFacebook->getAppSecret());

Upvotes: 5

Igy
Igy

Reputation: 43816

Check that the access token you're using works in Facebook's Debug Tool - there's nowhere in your code that's actually verifying the request to retrieve the app access token worked.

Also you need to check that the app is both

  1. Not in sandbox mode (may not be relevant, but worth checking)
  2. Not configured as 'Native/Desktop' in the advanced settings - the app access token isn't trusted in that configuration

Upvotes: 0

bobbiloo
bobbiloo

Reputation: 432

Did you check if OAuth is enabled in the php.ini?

Upvotes: 0

Related Questions