简体   繁体   English

Facebook通过Javascript SDK登录适用于移动和本地主机,但不适用于台式计算机

[英]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. 我通过Javascript SDK进行Facebook登录,Cookie设置为True ,在回调时我尝试获取该cookie,然后在我的数据库中添加或不添加访问者。

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). 它正在使用测试应用程序在移动设备和localhost上工作,但似乎并不适用于所有台式计算机(适用于我的iMac,而不适用于我的笔记本电脑)。 I cleared the cache and cookies on the concerned browser, does not change a thing. 我清除了相关浏览器上的缓存和cookie,没有改变一件事。

The $helper->getAccessToken gives me a NULL variable when I use var_dump to check it otherwise I get a blank page. 当我使用var_dump来检查它时, $helper->getAccessToken给了我一个NULL变量,否则我得到一个空白页面。

It used to work fine just before I put my website in SSL. 在我将我的网站放入SSL之前,它曾经很好地工作。

Any help much appreciated. 任何帮助非常感谢。

My website is online: www.KomuniD.com should you like to try to login and give me a feedback. 我的网站在线:www.KomuniD.com您是否想尝试登录并给我一个反馈。

My fb_login.js: 我的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: 我的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. 在我将我的网站放入SSL之前,它曾经很好地工作。

Well if you switched to HTTPS, then this: 好吧,如果你切换到HTTPS,那么:

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

should be an HTTPS URL as well. 也应该是HTTPS URL。

Ok, I discovered the answer. 好的,我发现了答案。

If I login from https://komunid.com it does not work. 如果我从https://komunid.com登录它不起作用。

Only works https://www.komunid.com 仅适用于https://www.komunid.com

Thx 谢谢

You should config your domain from http://yourdomain.com/ to http://yourdomain.com . 您应该将域名从http://yourdomain.com/配置为http://yourdomain.com
Working with me without ssl or https 没有ssl或https与我合作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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