如何在Facebook应用中发布和删除分数?

时间:2011-11-08 23:10:17

标签: php

我已阅读并试图在Facebook API上逐字实施分数系统,但我收到并收到错误

  

{“error”:{“message”:“不支持的帖子   请求 “” 类型 “:” GraphMethodException“}}

到目前为止,还没有人解决这个问题,也许我做错了。我现在正在回答关于其他人如何设法用代码示例进行处理的问题。

我想提前感谢你帮助我的努力。

index.php具有以下内容

require_once 'facebook.php';

$fbAppId = 'APPID';
$fbSecret = 'FBSECRET';
$fbCanvasUrl = 'CANVASURL';

 // Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId'  => $fbAppId,
'secret' => $fbSecret,
));

// Get User ID
$user = $facebook->getUser();

// Redirect to the canvas url if we're authorized and logged in
// You can also use $_REQUEST['signed_request'] for this
if($_GET['code']) {
echo "<script>top.location.href='".$fbCanvasUrl."'</script>";
exit;
}

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
 error_log($e);
  $user = null;
 }
 }
 else {
 $loginUrl = $facebook->getLoginUrl();
  echo "<script>top.location.href='".$loginUrl."'</script>";
 exit;
  }

它还包括javascript

        <script type="text/javascript">
    window.fbAsyncInit = function() {
        FB.init({appId: '<?=$fbAppId?>', status: true, cookie: true, xfbml: true,oauth: true});
    };
    (function() {
        var ts = new Date().getTime();
        var e = document.createElement("script");
        e.type = "text/javascript";
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js?ts='+ts;
        e.async = true;
        document.getElementById("fb-root").appendChild(e);
    }());
        </script>

所以.... index.php启动一切,我有一个Javascript文件,将播放游戏并设置一个cookie与分数和以下的PHP文件拿起cookie并设置分数.... / p>

    <?php
require_once('facebook.php');

$config = array(
    'appId' => 'APPID',
    'secret' => 'SECRET',
  );

  $facebook = new Facebook($config);


  $app_id = 'APPID';
  $app_secret = 'SECRET';
  $canvas_page_url = 'CANVASURL';

// Get the User ID
  $signed_request = parse_signed_request($_POST['signed_request'],
    $app_secret);

    $uid = $signed_request['user_id'];


  // Get an App Access Token
  $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);
  $app_access_token = $params['access_token'];


  //Get Score **************
  if(isset($_COOKIE['Sscore']))
        $cscore = $_COOKIE['Sscore'];
else
        exit;


$score=$cscore;
  // POST a user score
  //print('Publish a User Score<br/>');
  $score_URL = 'https://graph.facebook.com/' . $uid . '/scores';
  $score_result = https_post($score_URL,
    'score=' . $score
    . '&access_token=' . $app_access_token
  );
 printf('<br/>%s<br/>',$score_result);

  function https_post($uri, $postdata) {
    $ch = curl_init($uri);
   // curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    $result = curl_exec($ch);
    curl_close($ch);

    return $result;
  }
  function parse_signed_request($signed_request, $secret) {
    list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

    // decode the data
    $sig = base64_url_decode($encoded_sig);
    $data = json_decode(base64_url_decode($payload), true);

    if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
      error_log('Unknown algorithm. Expected HMAC-SHA256');
      return null;
    }

    // check sig
    $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
    if ($sig !== $expected_sig) {
      error_log('Bad Signed JSON signature!');
      return null;
    }

    return $data;
  }

  function base64_url_decode($input) {
    return base64_decode(strtr($input, '-_', '+/'));

  } 


?>

0 个答案:

没有答案