Facebook PHP SDK没有读取cookie值

时间:2016-03-16 14:56:05

标签: javascript php facebook cookies

我对Facebook SDK很陌生,但我尝试实施Facebook身份验证,所以我使用Javascript SDK和PHP SDK以及使用Javascript Helper进行操作,如文档所示,但这并不是&# 39; t work,因为这会返回消息没有找到访问令牌,但是我已经检查过并且生成了cookie,这可能是什么问题?

JAVASCRIPT

window.fbAsyncInit = function () {
    FB.init({
        appId: 'xxxxxxxx',
        cookie: true,
        version: 'v2.5'
    });
    FB.getLoginStatus(function (response) {
        if (response.status === 'connected') {
            $('#fb-login').text('Conexion autorizada').off('click');
        } else if (response.status === 'not_authorized') {
            $('#fb-login').removeClass('disabled').text('Autorizar App');
        } else {
            $('#fb-login').removeClass('disabled').on('click');
        }
    });
};
(function (d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id))
        return;
    js = d.createElement(s);
    js.id = id;
    js.src = "//connect.facebook.net/es_LA/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

$('#fb-login').click(function () {
    FB.login(function (response) {
        if (response.authResponse) {
            FB_Auth();
        } else {
            alert('Inicio de sesión cancelado o no autorizado');
        }
    }, {scope: 'user_posts,email,contact_email,publish_actions,public_profile', return_scopes: true});
});

function FB_Auth() {
    $.ajax({
        url: 'auth/fb_login.php',
        method: 'POST',
        data: {action: 'login'},
        success: function (server_response) {
            console.log("La respuesta fue: \n" + server_response);
            if (server_response === 'ok') {
                window.location.href = "appmenu.php";
            }
        },
        error: function (server_response) {
            console.log("Error en respuesta servidor:" + server_response);
        }
    });
}

AJAX函数调用的PHP

PHP

    <?php
    require '../controladoras/globals.php';
    require '../Facebook/autoload.php';

    $action = filter_input(INPUT_POST, 'action', FILTER_SANITIZE_STRING);

    $fb = new Facebook\Facebook([
        'app_id' => 'xxxxxxxxx',
        'app_secret' => 'xxxxxxxxxxxx',
        'default_graph_version' => 'v2.5',
            ]);

    $helper = $fb->getJavaScriptHelper();

    try {
        $accessToken = $helper->getAccessToken();
    } catch (Facebook\Exceptions\FacebookResponseException $e) {
        // When Graph returns an error
        echo ('Graph returned an error: ' . $e->getMessage());
    } catch (Facebook\Exceptions\FacebookSDKException $e) {
        // When validation fails or other local issues
        echo ('Facebook SDK returned an error: ' . $e->getMessage());
    }


   if (! isset($accessToken)) {
  echo 'No cookie set or no OAuth data could be obtained from cookie.';
  exit;
    } else {
        var_dump($accessToken->getValue());
        $fb_auth = new Sesiones();
        switch ($action) {
            case 'login':
                $params = [["param" => "authLvl", "val" => 1], ["param" => "fb_access_token", "val" => (string) $accessToken]];
                $fb_auth->IniciarSesion($params);
                echo ('ok');
                break;
            case 'logout';
                $fb_auth->CerrarSesion();
                exit;
                break;
        }
    }

0 个答案:

没有答案