使用Facebook SDK PHP登录-用户必须单击两次登录 [英] Login With Facebook SDK PHP - User Must Click Login Twice

查看:68
本文介绍了使用Facebook SDK PHP登录-用户必须单击两次登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP Facebook SDK来通过Facebook登录用户.

I am trying use the PHP Facebook SDK to log users in with facebook.

我首先使用了从GitHub下载的.zip中提供的示例文件.

I have started off by using the example file provided in the .zip I downloaded from GitHub.

当用户单击登录时,他们将进入身份验证页面.在允许我的应用程序之后,它们被重定向回原始页面,该页面应在该页面上打印用户对象.但事实并非如此.再次单击登录会跳过身份验证页面(因为用户已经允许了该应用程序),然后打印该对象.

When users click login they are taken to the authentication page. After the allow my app they are redirected back to the original page where it should print_r the user object. But it does not. Clicking login again skips the authentication page (as the user has already allowed the app) and then the object is printed.

任何帮助将不胜感激.

<?php 
/**
 * Copyright 2011 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

require '../src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => '1111111111',
  'secret' => 'xxxxxxxxxxxxxxxxxxxxxxx'
));


// Get User ID
$user = $facebook->getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
  echo '1 <br>';
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    echo '2 <br>';
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    echo '3 <br>';
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  echo '4 <br>';
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  echo '5 <br>';
  $loginUrl = $facebook->getLoginUrl(array(
      'scope' => 'email,read_stream,publish_stream',
      'display' => 'touch'
      ));
}

// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');

?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <head>
    <title>php-sdk</title>
    <style>
      body {
        font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
      }
      h1 a {
        text-decoration: none;
        color: #3b5998;
      }
      h1 a:hover {
        text-decoration: underline;
      }
    </style>
  </head>
  <body>
    <h1>php-sdk</h1>

    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
      <div>
        Login using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
      </div>
    <?php endif ?>

    <h3>PHP Session</h3>
    <pre><?php echo '6 <br>'; print_r($_SESSION); ?></pre>

    <?php if ($user): ?>
      <h3>You</h3>
      <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

      <h3>Your User Object (/me)</h3>
      <pre><?php echo '7 <br>'; print_r($user_profile); ?></pre>
    <?php else: ?>
      <strong><em>You are not Connected.</em></strong>
    <?php endif ?>

    <h3>Public profile of Naitik</h3>
    <img src="https://graph.facebook.com/naitik/picture">
    <?php echo $naitik['name']; ?>
  </body>
</html>

推荐答案

    <div id="fb-root"></div>
    <script src="//connect.facebook.net/en_US/all.js"></script>
    <script>
      window.fbAsyncInit = function() {
        // init the FB JS SDK
        FB.init({
          appId      : 'YOUR_APP_ID', // App ID from the App Dashboard
          status     : true, // check the login status upon init?
          cookie     : true, // set sessions cookies to allow your server to access the session?
          xfbml      : true  // parse XFBML tags on this page?
        });

        FB.Event.subscribe('auth.login', function(response) {
        window.location.href=window.location.href;
    }

      };

      // Load the SDK's source Asynchronously
      // Note that the debug version is being actively developed and might 
      // contain some type checks that are overly strict. 
      // Please report such bugs using the bugs tool.
      (function(d, debug){
         var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement('script'); js.id = id; js.async = true;
         js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
         ref.parentNode.insertBefore(js, ref);
       }(document, /*debug*/ false));
    </script>

这篇关于使用Facebook SDK PHP登录-用户必须单击两次登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆