1

在我的 javascript 中,我有一个点击事件,它触发了对我发送通知的 php 页面的 ajax 调用。我选择这样做是因为文档建议不要在任何客户端代码中使用您的应用程序密码,并且通知参数需要您只能使用应用程序密码获取的访问令牌。

我遇到的问题是,即使我已经登录,$facebook->getUser() 在 php 中返回 0,所以我之后发送通知的 api 调用将不起作用。我的用户已经通过客户端代码登录,所以我如何将消息发送到他们已登录的 php,以便可以发送通知。

//JS 

                            $.ajax({
                        url : "http://xxxxxo/bn/notification.php",
                        type : 'POST',
                        data: {notify: notify },
                        success : function (result) {

                                console.log(result); 

                        },
                        error : function () {
                           alert("error sending notification");
                        }
                      });//closes ajax 

//PHP

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

$APPLICATION_ID = '1402xxxxx7';
$APPLICATION_SECRET = 'ce71d6bbxxxxx5f55a';
$fb_app_url = "http://apps.facebook.com/myAPP";

$config = array();
$config['appId'] = $APP_ID;
$config['secret'] = $APP_SECRET;
$config['cookie'] = true;

$facebook = new Facebook($config) or die('Error is here!');

$facebook = new Facebook(array(
            'appId' => $APP_ID,
            'secret' => $APP_SECRET,
            'fileUpload' => true
        ));     

$notify = $_REQUEST['notify'];

$userid = $facebook->getUser();


/*IF WE HAVE A LOGGED IN USER AND THE 'NOTIFY' REQUEST VALUE, THEN SEND THE NOTIFICATION.
BUT MY USER ID IS 0. HOW DO I GET PHP TO RECOGNIZE ME AS LOGGED IN WITHOUT HAVING TO FORCE MY USER TO LOG IN VIA PHP AFTER THEY'VE ALREADY LOGGED IN CLIENT SIDE?*/

if($userid && $notify){

    $token_url ="https://graph.facebook.com/oauth/access_token?" .
                "client_id=" . $APP_ID .
                "&client_secret=" . $APP_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'
);

$sendnotification = $facebook->api('/1622649653/notifications', 'post', $data);

}else{

//handle error

} 



?>
4

2 回答 2

1

我注意到的第一件事是您将您的应用程序 ID 定义为$APPLICATION_ID但将其用作$APP_ID(您的应用程序密码也是如此)。但是由于您没有提及任何错误并$facebook->getUser();执行,我猜这只是一个糟糕的复制粘贴。

现在为了回答这个问题,我假设您使用的是最新版本的 JS 和 PHP SDK。这些使用 oauth 2.0 并更改了将登录信息从 JS 传递到 PHP 的方式。

根据Facebook 开发人员博客,在您的 JS 配置中删除$config['cookie'] = true;和设置应该可以工作。只需确保在登录后刷新站点即可。oauthtrue

我在自己的项目中找到的解决方案是完全禁用 cookie,只需将访问令牌传递给我的 PHP 脚本。
在您的 JS 中像这样调用您的 PHP 脚本(确保在 JS 登录后调用它!):

$.ajax({
    url : "http://xxxxxo/bn/notification.php",
    type : 'POST',
    data: {
        notify: notify,
        token: FB.getAuthResponse()['accessToken'] // add your access token
    },
    success : function (result) {
        console.log(result); 
    },
    error : function () {
        alert("error sending notification");
    }
});

创建 FB 对象后,在您的 PHP 脚本中添加它。

$facebook->setAccessToken($_POST['token']); // set the users access token

这样做也将摆脱登录后刷新网站的任何需要。

于 2013-08-07T14:15:36.903 回答
1

是的,这是结合 AJAX 使用 PHP SDK 时的常见问题:

当您发出 AJAX 请求时,PHP SDK 会删除存储授权信息的 cookie,然后下一次调用getUser将只返回 0,因为此方法试图在这些 cookie 中查找当前用户 id——显然在OAuth 2.0 规范要求这种行为来防止某种点击劫持攻击。

但信息仍将存储在会话中,因此您可以从那里读取用户 ID(以及用户访问令牌,如果您需要的话):

$user_id = $_SESSION['fb_YourAppIdHere_user_id'];
$user_access_token = $_SESSION['fb_YourAppIdHere_access_token'];

替换YourAppIdHere为您的应用程序 ID(因此它变为fb_1234567890_user_idresp. fb_1234567890_access_token)以获取这些会话密钥的正确名称。

于 2013-08-07T14:16:30.430 回答