简体   繁体   中英

Facebook login via Javascript SDK works on mobile and localhost but not on desktop computers

It is very odd. I made a Facebook login through Javascript SDK with Cookie set to True , and on callback I try to get that cookie, and then add or not the visitor in my database.

It is working on mobiles and localhost with test app, but does not seems to work on all desktop computers (works on my iMac, not on my laptop). I cleared the cache and cookies on the concerned browser, does not change a thing.

The $helper->getAccessToken gives me a NULL variable when I use var_dump to check it otherwise I get a blank page.

It used to work fine just before I put my website in SSL.

Any help much appreciated.

My website is online: www.KomuniD.com should you like to try to login and give me a feedback.

My fb_login.js:

logInWithFacebook = function() {
  if ("standalone" in navigator && navigator.standalone) {
    $("a").click(function (event) {
        event.preventDefault();
        window.location = $(this).attr("href");
    });
  }

  FB.login(function(response) {
    if (response.authResponse) {
      window.location.replace('http://www.komunid.com/fb-callback.php');
    } else {
      alert('User cancelled login or did not fully authorize.');
    }
  },
  {
    scope: 'public_profile,email,user_friends',
    auth_type: 'rerequest',
  });

  return false;
};

 window.fbAsyncInit = function() {
    FB.init({
      appId      : 'my-app-id',
      status     : true,
      xfbml      : true,
      cookie     : true,
      version    : 'v2.6'
    });
  };

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

My fb_callback.php:

<?php
session_start();

require __DIR__ . '/init.php';

$fb = new Facebook\Facebook([  
  'app_id' => 'my-app-id',  
  'app_secret' => 'my-app-secret',
  'default_graph_version' => 'v2.6',
  'cookie' => true 
  ]);

$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();  
  var_dump($helper->getPersistentDataHandler());
  var_dump($_GET, $_SESSION);
  exit;  
}  

if (!isset($accessToken)) {  
  if ($helper->getError()) {  
    header('HTTP/1.0 401 Unauthorized');  
    echo "Error: " . $helper->getError() . "\n";
    echo "Error Code: " . $helper->getErrorCode() . "\n";
    echo "Error Reason: " . $helper->getErrorReason() . "\n";
    echo "Error Description: " . $helper->getErrorDescription() . "\n";
  } else {  
    header('HTTP/1.0 400 Bad Request');  
    echo 'Bad request';  
  }  
  exit;  
}  

It used to work fine just before I put my website in SSL.

Well if you switched to HTTPS, then this:

window.location.replace('http://www.komunid.com/fb-callback.php');

should be an HTTPS URL as well.

Ok, I discovered the answer.

If I login from https://komunid.com it does not work.

Only works https://www.komunid.com

Thx

You should config your domain from http://yourdomain.com/ to http://yourdomain.com .
Working with me without ssl or https

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM