Facebook JS SDK does not set cookie?

I use the code on the last page of the FB developers. In the JS SDK, I can enter, get an access token, make api calls, etc.

The PHP SDK getJavaScriptHelper () returns empty for the access token.

I checked the fbsr_ {app_id} cookie and it isn’t there. I tried to create my own test cookie with JS and get it, and it is configured, and I can access it with PHP, so this is not a browser problem.

I installed cookie: true,in the JS SDK, I use the latest PHP SKD v5 and Graph Api v2.8 (the same is installed in my developer toolbar).

I am using CodeIgniter for a local wamp server.

I can neither think nor find anything else, I spent all day on this. Any help would be appreciated!

the code:

<html>
<body>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>

<p><a href="#" onClick="logInWithFacebook()">Log In with the JavaScript SDK</a></p>

<script>

  logInWithFacebook = function() {
    FB.login(function(response) {
      if (response.authResponse) {
        console.log('You are logged in &amp; cookie set!');
        console.log(response);
        // Now you can redirect the user or do an AJAX request to
        // a PHP  that grabs the signed request from the cookie.
        $.get('<?php echo base_url() ?>index.php/test_facebook/js_login', function(resp){
            console.log('Get response' + resp);
        });
      } else {
        console.log('User cancelled login or did not fully authorize.');
      }
    });
    return false;
  };
  window.fbAsyncInit = function() {
    FB.init({
      appId: '{app-id}',
      cookie: true, 
      version: 'v2.8'
    });
  };

  (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/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));
</script>
</body>
</html>

and test_facebook / js_login:

<?php
class Test_facebook extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->view('test_facebook');
    }
    public function js_login(){
        # /js-login.php
        require_once( 'vendor/autoload.php' );

        $fb = new Facebook\Facebook([
          'app_id' => '{app-id}',
          'app_secret' => '{app-secret}',
          'default_graph_version' => 'v2.8',
          ]);

        $helper = $fb->getJavaScriptHelper();

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

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

        // Logged in
        echo '<h3>Access Token</h3>';
        var_dump($accessToken->getValue());
    }
}

UPDATE

, . FB devs . http://localhost:8383/test4/index.php/test_facebook/js_login, No cookie set or no OAuth data could be obtained from cookie.

+4

Source: https://habr.com/ru/post/1662316/


All Articles