1

这个问题与我没有得到足够帮助的赏金问题有关。无法通过 ajax 调用向 php 发送 facebook 通知

我需要通过 ajax 将 accessToken 传递给 php。我在脚本中调用 getLoginStatus,从响应中获取访问令牌,然后将其包含在 ajax 的发布数据中。虽然它给了我一个错误,但我不知道为什么。是语法问题吗?

FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
        var accessToken = response.authResponse.accessToken;
        console.log(accessToken);
        $.ajax({
            url : "http:/xxxxxxio/bn/notification.php",
            type : 'POST',
            data: { token: accessToken },
            success : function (result) {
                console.log(result); 
            },
            error : function () {
                alert("error sending notification");
            }
        });
    } else if (response.status === 'not_authorized') {
        // the user is logged in to Facebook, 
        // but has not authenticated your app
    } else {
        // the user isn't logged in to Facebook.
    }
});

//PHP

<?php
require_once(dirname(__FILE__).'/php-sdk/facebook.php') ;

$APPLICATION_ID = '14xxxxxx15107';
$APPLICATION_SECRET = 'ce71dxxxxxxx040971a45f55a';
$fb_app_url = "http://apps.facebook.com/thebringernetwork";

$config = array();
$config['appId'] = $APPLICATION_ID;
$config['secret'] = $APPLICATION_SECRET;
$config['cookie'] = true;


$facebook = new Facebook($config) or die('Error is here!');
$facebook = new Facebook(array(
    'appId' => $APPLICATION_ID,
    'secret' => $APPLICATION_SECRET,
    'fileUpload' => true
)); 

$facebook->setAccessToken($_POST['token']);     

$userid = $facebook->getUser();

echo $userid;

$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/thebringernetwork/',
    'access_token'=> $app_token,
    'template'=> 'test'
);

$facebook->api('/1xxxx653/notifications', 'post', $data);
?>
4

0 回答 0