1

参照: https://developers.facebook.com/docs/howtos/login/extending-tokens/

状態:

デフォルトでは、ほとんどのアクセス トークンの有効期間は有限で、通常は 1 ~ 2 時間です。有効期限が切れた後もこれらのトークンを引き続き使用するには、トークンを延長する必要があります。

では、facebook php SDK に示されているすべての例で、ログインが数分後に期限切れになり、その後新しいログインが必要になるのはなぜですか?

ありがとう

編集

これは一例のコードです ( https://github.com/facebook/facebook-php-sdk/blob/master/examples/with_js_sdk.php )

    <?php

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

$facebook = new Facebook(array(
  'appId'  => '344617158898614',
  'secret' => '6dc8ac871858b34798bc2488200e503d',
));

// See if there is a user from a cookie
$user = $facebook->getUser();

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
    $user = null;
  }
}

?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <body>
    <?php if ($user) { ?>
      Your user profile is
      <pre>
        <?php print htmlspecialchars(print_r($user_profile, true)) ?>
      </pre>
    <?php } else { ?>
      <fb:login-button></fb:login-button>
    <?php } ?>
    <div id="fb-root"></div>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId: '<?php echo $facebook->getAppID() ?>',
          cookie: true,
          xfbml: true,
          oauth: true
        });
        FB.Event.subscribe('auth.login', function(response) {
          window.location.reload();
        });
        FB.Event.subscribe('auth.logout', function(response) {
          window.location.reload();
        });
      };
      (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol +
          '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
      }());
    </script>
  </body>
</html>
4

1 に答える 1

2

私も最近これに遭遇しました。と関係があると思う/meので、 に変更してみました/user_id。使用したコードは次のとおりです。

require_once 'facebook-php/src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => '13********',
  'secret' => '7a***********',
));

$userId = $facebook->getUser();
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">
<body>
<div id="fb-root"></div>
<script>
    window.fbAsyncInit = function() {
      FB.init({
        appId      : '13**********', // App ID
        status     : true, // check login status
        cookie     : true, // enable cookies to allow the server to access the session
        xfbml      : true  // parse XFBML
      });
    FB.Event.subscribe('auth.login', function(response) {
      window.location.reload();
    });
    };
    (function(d){
       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.js";
       ref.parentNode.insertBefore(js, ref);
     }(document));
  </script>   <?
if ($userId) { 
    $userInfo = $facebook->api('/' . $userId); 
?>
<img src="http://graph.facebook.com/<?php echo $userId; ?>/picture" align="left" width="24px" height="24px" style="padding: 2px 10px 2px 5px"> <div style="margin:0 0 0 0; padding:0 0 0 0; display:inline; height:24px; line-height:28px"><b>Hi there, <?php echo $userInfo['name']; ?>!</b></div>
<? } else { ?>
  <div class="fb-login-button" style="display:inline; position:relative; top:3px" scope="email,user_birthday,publish_stream,offline_access"></div>
<? } ?>
</body>
</html>

そのコードは私にとってはうまく機能し、Facebook にサインインしている限り、サイトに表示されます。

于 2013-02-01T16:48:42.073 に答える