Cordova,Facebook Javascript / Jquery SDK和PHP。服务器端无法看到cookie

时间:2016-02-24 17:02:46

标签: javascript php facebook cordova cookies

我正在使用netbeans在Cordova应用程序上开发facebook JS + PHP用户登录,使用NB连接器测试chrome。

这是客户端代码:

 $(document).ready(function () {
            $.ajaxSetup({cache: true});
            $.getScript('//connect.facebook.net/en_US/sdk.js', function () {
                FB.init({
                    appId: 'xxxxxxxxxxxxxxxxxx',
                    version: 'v2.5', // or v2.0, v2.1, v2.2, v2.3
                    status     : true,
                    cookie: true
                });
                $('#loginbutton,#feedbutton').removeAttr('disabled');
                FB.getLoginStatus(function (response) {
                    if (response.status === 'connected') {
                        console.log("logged in");
                    } else {
                        console.log("this user is not logged in yet");
                    }
                });
            });
        });
        function doFacebookLogin() {
            FB.getLoginStatus(function (response) {
                if (response.status === 'connected') {
                    console.log("token " + response.authResponse.accessToken);

                    $.ajax({
                        url: "http://mywebsite/fb_login.php",
                        crossDomain: true,
                        jsonpCallback: 'callb',
                        dataType: 'jsonp',
                        success: function (response) {
                            console.log('callback success' + response);
                            console.log(response);

                        },
                        error: function (xhr, status, error) {
                            console.log(status + '; ' + error);
                            alert(status + ' | ' + error + ' | ' + xhr);
                        }
                    });
                } else {
                    FB.login(function (response) {
                        if (response.status === 'connected') {
                            FB.getLoginStatus(function (response) {
                                if (response.status === 'connected') {
                                    console.log("token " + response.authResponse.accessToken);
                                    $.ajax({
                                        url: "http://mywebsite/fb_login.php",
                                        crossDomain: true,
                                        jsonpCallback: 'callb',
                                        dataType: 'jsonp',
                                        success: function (response) {
                                            console.log('callback success' + response);
                                        },
                                        error: function (xhr, status, error) {
                                            console.log(status + '; ' + error);
                                            alert(status + ' | ' + error + ' | ' + xhr);
                                        }
                                    });
                                }
                            });
                        } else if (response.status === 'not_authorized') {
                            console.log("this user does not have approved this app yet!");
                        } else {
                            console.log("this user is not even logged to facebook!");
                        }
                    });
                }
            });

        }
        function callb(json) {
            console.log("Callback was called!")
        }

如果用户没有登录或是第一次,有一个按钮供用户手动登录facebook。

这是服务器端代码:

    error_reporting(-1);
ini_set('display_errors', 'On');
# /js-login.php
require_once __DIR__ . '/facebook-sdk-v5/autoload.php';

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

$helper = $fb->getJavaScriptHelper();
$response = "";
//$response.= print_r($_COOKIE,true);
try {
    $accessToken = $helper->getAccessToken();
} catch (Facebook\Exceptions\FacebookResponseException $e) {
    // When Graph returns an error
    $response .= 'Graph returned an error: ' . $e->getMessage();
    outputJsonP($response);
    exit;
} catch (Facebook\Exceptions\FacebookSDKException $e) {
    // When validation fails or other local issues
    $response .= 'Facebook SDK returned an error: ' . $e->getMessage();
    outputJsonP($response);
    exit;
}

if (!isset($accessToken)) {
    $response .= 'error: No cookie set or no OAuth data could be obtained from cookie.';
    outputJsonP($response);
    exit;
}

// Logged in
//var_dump($accessToken->getValue());

$_SESSION['fb_access_token'] = (string) $accessToken;
outputJsonP($response);

function outputJsonP($response) {
    $myArray = array(
        "response" => $response,
    );
    $myJSONString = json_encode($myArray);
    echo 'callb(' . $myJSONString . ');';
}

我总是获得

  

错误:未设置Cookie或无法从Cookie获取OAuth数据。

所以我猜或者cookie没有进入服务器端,或者oAuth进程进展不顺利。 我查看了

的结果
  

的print_r($ _ COOKIE,真)

但我找不到cookie。我也不知道该搜索什么......

可能jsonP ajax请求不允许cookie进入服务器? 如果是这样我怎么能这样做?

有人可以帮助我更好地发现问题或解决问题吗?谢谢!

编辑:也许可以与此相关?Cannot set cookies in Javascript

0 个答案:

没有答案