Webサイト用の新しいFacebookAPIに移行しようとしていますが、スムーズに動作させることができません。JavascriptとPHPSDKを一緒に使用したいのですが、問題が発生しています。認証はできますが、ページが自動的に更新されません。
SOとFacebookのドキュメント全体を検索しましたが、それでも正しく機能させることができません。http://developers.facebook.com/blog/post/534/を読み、最新のPHP SDK(3.1.1)をダウンロードし、基本的にFacebookから前述の投稿の例をコピーしました。アプリの「移行」設定で正しい設定を行ったのですが、これが問題の原因である可能性があります。画像を投稿できないので、設定は次のとおりです。
- 非推奨のAPIを削除:有効
- ログインシークレットの使用を強制する:有効
- 暗号化されたアクセストークン:有効
- 拡張認証ダイアログ:有効
- (他はすべて無効になっています)
コードは次のとおりです。
<?php
require 'php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
));
// 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_profile) { ?>
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>