Search code examples
facebookfacebook-graph-apinotificationsaccess-token

post a notification with new api and access_token


I am trying to post a notification to myself through the new notifications api

This is how I get my access token (Wich works, because i use it for some other features)

<?php
    $code = $_REQUEST["code"];
     //auth user
     if(empty($code)) {
         $dialog_url = 'https://www.facebook.com/dialog/oauth?client_id=appid&redirect_uri=' . urlencode('http://funcook.com/facebook.php') ;
        echo("<script>top.location.href='" . $dialog_url . "'</script>");
      }
      //get user access_token
      $token_url = 'https://graph.facebook.com/oauth/access_token?client_id=appid&redirect_uri=' . urlencode('http://funcook.com/facebook.php') 
        . '&client_secret=[REMOVED SECRET]&code=' . $code;
      $access_token = file_get_contents($token_url);
?>

And this is how I'm trying to send the notification:

<script>
     FB.api('/<?=$uid?>/notifications?<?=$access_token?>&template=hi toni&href=http://mywebsite.com/?yiha', 'post', {}, function (response) {
             if (!response || response.error) {
                    console.log('Error occured:' + response.error.message);
    }else{     console.log('Post ID: ' + response.id);
             }

        });
</script>

This is what the console logs:

Escaping unescaped character \x20 from "/652286316/notifications?access_token=&expires=5883&template=hi toni&href=http://mywebsite.com/?yiha" facebook.php:222Error occured:(#15) This method must be called with an app access_token.

What am I missing?


Solution

  • Looks like you're using the User access token instead of an app access token - posting notifications needs to be done with the App Access Token

    There's a brief guide to using the app access token for posting Open Graph actions here: https://developers.facebook.com/docs/opengraph/using-app-tokens/

    The steps to retrieve the token are applicable here, too.

    More details: https://developers.facebook.com/docs/howtos/login/login-as-app/