FB.login with PHP API

I created a Canvas page that does FB.login with the click of a submit form button. During the next request, he tries to access user data through $ facebook-> api ('/ me') (the latest version of the API from Github). It works in Firefox and Chrome, but not in Safari and IE, where the API fails with the "required authentication token." Has anyone already had this problem or figured out what might cause it?

BR Philipp

edit:

I call FB.login inside the form submit button click event:

$('.form-submit', this).click(function() {
  FB.getLoginStatus(function(response) {
    if (response.session) {
      form.submit();
    } else {
      FB.login(function(response) {
        if(response.session && (permissions == '' || response.perms)) {
          form.submit();
        }
        else {
        }
      },{perms:permissions});
    }
  });
  return false;
});

On the server side, just create a php-api object and try to get user data:

$facebook = new Facebook(array(
  'appId' => $appid,
  'secret' => $appsecret,
  'cookie' => TRUE,
));
if ($facebook) {
  try {
    $me = $api->api('/me');
  }
  catch (Exception $exc) {
    // Failure in Safari and IE due to invalid auth token
  }
}

The signed_request file is transferred inside the hidden form element.

+5
source share
3

JS FB. oauth getLoginUrl php fb api.

, PHP, , , getLoginUrl , / ( ).

? HOURS, FB JS , , .

+5

, .

, , Javascript . Javascript Facebook.com, , . , ; , facebook .

. FB.login , access_token. , , script - . :

// Hold the access token
var js_access_token = "";

// Connect to facebook
FB.login(function(response) {
  if (response.session) {
    if (response.perms) {
      // user is logged in and granted some permissions.
      // Save the access token
      js_access_token = response.session.access_token;
      // Do stuff on login
    }
  }
});

. ajax.

// Communication back to server.
$.ajax({
  url: 'myurl.php',
  data: {
    js_access_token: js_access_token // Including the js_access_token
  },
  success: function(data) {
    console.log(data);
 }
});

PHP - :

$facebook = new Facebook(array(
  'appId' => $appid,
  'secret' => $appsecret,
  'cookie' => TRUE,
));
if ($facebook) {

  // If we get the access token from javascript use it instead
  if (isset($_REQUEST['js_access_token']) && $_REQUEST['js_access_token']) {
    $facebook->setAccessToken($_REQUEST['js_access_token']);
  }

  try {
    $me = $api->api('/me');
  }
  catch (Exception $exc) {
    // Failure in Safari and IE due to invalid auth token
  }
}

,

+8

I liked Conor's answer, because I had to transfer the access token from the client side to the server, because it did not work in Safari (problems with cookies, which I suppose). But this is an old question, so some things had to change. Now the vars are different, and, as Ollandan noted, we should not send access tokens as GET parameters.

Anyway, this is what I got in case it helps someone

<a id="start-button" href="#">Start</a>
<form id="entry-form" action="nextpageurl" method="post">
    <input type="hidden" name="access_token" />
</form>

<script>
$(document).ready(function() {
    $('#start-button').click(function(e) {
        e.preventDefault();
        FB.login(function (response) {
            if (response.authResponse) {
                $('#entry-form input').val(response.authResponse.accessToken);
                $('#entry-form').submit();
            } else {
                alert('Permissions required');
            }
        }, {});
    });
});
</script>

and then in PHP, almost the same as for Conor, but getting the token from $ _POST var.

0
source

All Articles