I’m developing a PHP game and would like to post players highscores to their own facebook wall /timeline.
I’ve set up a Facebook App and the PHP code I’m using to POST the score is (as provided by Facebook itself):
<?php
require 'facebook-sdk/facebook.php';
$app_id = MY_APP_ID;
$app_secret = MY_APP_SECRET;
$score = 1500; // this is gonna be passed someway...
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$user = MY_USER_ID; // to be replaced with a call to $facebook->getUser()
$app_access_token = get_app_access_token($app_id, $app_secret);
$facebook->setAccessToken($app_access_token);
$response = $facebook->api('/' . $user . '/scores', 'post', array(
'score' => $score,
));
print($response);
// Helper function to get an APP ACCESS TOKEN
function get_app_access_token($app_id, $app_secret) {
$token_url = 'https://graph.facebook.com/oauth/access_token?'
. 'client_id=' . $app_id
. '&client_secret=' . $app_secret
. '&grant_type=client_credentials';
$token_response =file_get_contents($token_url);
$params = null;
parse_str($token_response, $params);
return $params['access_token'];
}
?>
Of course there is a login and install section which I have omitted, asking the user to login and grant ‘publish_stream‘ and ‘publish_actions‘ privileges to the app.
This is working with success, the response variable outputs 1.
I can see the posted score using the Facebook Graph API Explorer so I assume everything is really working fine and smooth.
The problem is that I am not able to see the supposedly posted user-story anywhere on Facebook.
Reading the documentation it seems to me that a user story has to be automatically published when one saves a score. As an example, have a look here or here.
Does anyone solved this problem already? Do you see something that I might have missing? Can you please point me at the right direction to solve this issue?
Any help will be highly appreciated.
You write
Scores are not automatically published. They are only published under certain conditions, namely when a user:
In your code you post the score 1,500 everytime. After the first time you post it, when you post it again repeatedly for testing, your post request will be successful but the score will not be published again since it is not a new high.
Sources:
Facebook Developers: Games Tutorial.
Facebook Developers Developer Blog: Games Update: Expanding distribution for Scores and Achievements