Notifications API with PHP SDK

10,747

Solution 1

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());

Solution 2

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

Solution 3

$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

Share:
10,747
Martha Smithers
Author by

Martha Smithers

Updated on November 22, 2022

Comments

  • Martha Smithers
    Martha Smithers over 1 year

    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?

  • Martha Smithers
    Martha Smithers over 11 years
    It is not in sandbox mode and configured as web.
  • Igy
    Igy over 11 years
    Wait, were you just passing the result of file_get_contents into a variable and using it directly? Doesn't that also return an additional string before the token itself?