6

I use code from up-to-date FB developers page. In JS SDK I can login, get access token, make api calls etc.

PHP SDK getJavaScriptHelper() returns empty for access token.

I have checked for fbsr_{app_id} cookie and it is not there. I tried to create my own test cookie with JS and retrive and it is set and I can access it from PHP so it is not browser problem.

I have set cookie: true, in JS SDK, I use latest PHP SKD v5 and Graph Api v2.8(The same is set in my developers dashboard).

I use CodeIgniter on wamp server localhost.

I can not think or find anything else, have spent all day on this one. Any help would be appreciated!

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 script 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

I countinued looking for solution I found this in FB devs page about Server IP Whitelist. I listed http://localhost:8383/test4/index.php/test_facebook/js_login but still returning No cookie set or no OAuth data could be obtained from cookie.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.