0

好的,正如标题所示,我一直在尝试让 Facebook 的 OAuth 在我的 localhost/ 上运行,我一直在努力解决各种问题,但我只是不知道我的最新问题是什么,即页面加载,并且如果用户已登录,它会显示注销 URL 和 UID . 长话短说,我似乎无法让在我的本地主机中运行的网站将我从 Facebook 和我的网站上注销!任何想法都会有很大的帮助。这是我的代码

<?php

session_start();
// store session data

include "src/facebook.php";

$facebook = new Facebook(Array(
    "appId" => "xxx",
    "secret" => "xxx"
));

$user = $facebook->getUser();

echo "UID: " . $user . "<br/>";

    // THIS WAS MY FIRST TRY WHICH DIDNT SUCCEED
// if ($user) {
//  $logoutUrl = $facebook->getLogoutUrl();
//  echo "<a href='" . $logoutUrl . "'>Logout</a>";
// } else {
//  $loginUrl = $facebook->getLoginUrl();
//  echo "<a href='" . $loginUrl . "'>Login</a>";
// }

// WITH THE ABOVE CODE I GET:

// An error occurred with PHP-SDK. Please try again later.
// API Error Code: 191
// API Error Description: The specified URL is not owned by the application
// Error Message: Invalid redirect_uri: Given URL is not allowed by the Application configuration.

    // THIS WAS MY SECOND TRY AFTER SEARCHING FOR INFO, 
    // ALL I DID WAS MANUALLY ADD THE
    // REDIRECT URI AND ADDED ? , THE REASON IS IF I DIDN'T ADD IT,
    // IT WOULD GIVE ME AN CSRF ERROR
    // STATING THAT THE STATE VALUE IS NOT CORRECT, I SEARCHED FOR THE SOLUTION
    // FOR THAT PROBLEM WITH NO LUCK SO I TRIED THIS

if ($user) {
    $logoutUrl = $facebook->getLogoutUrl();
    echo "<a href='" . $logoutUrl . "'>Logout</a>";
} else {
    $loginUrl = $facebook->getLoginUrl(Array(
        "redirect_uri" => "http://localhost/php-sdk/index.php?"
    ));
    echo "<a href='" . $loginUrl . "'>Login</a>";
}

// WITH THE ABOVE CODE I NOW GET A UID AND THE LOGOUT URL DOES APPEAR, BUT ONCE I CLICK IT
// THE USER IS LOGGED OUT LIKE HE SHOULD BUT THEN ON REDIRECT OR RELOAD THE UID STILL 
// APPEARS AND SO DOES THE LOGOUT LINK, EVEN THOUGH THE USER IS NOT LOGGED IN!!

?>
4

1 回答 1

2

这是因为 facebook sdk 商店的用户 ID在您的 PHP 会话中,而发生在 facebook 服务器上的注销过程无法为您清除。您可以通过检查输出来查看会话中保存的内容var_dump($_SESSION);

要检测存储在会话中的 uid 是否仍然登录,您可以尝试使用 SDK 对象从图形 api 中获取某些内容,例如 ( $facebook->api('/me')) 并捕获异常,并在发生异常时清除会话。

另一种解决方案可能是nextgetLogoutUrl()调用中添加一个(就像在 中一样getLoginUrl()),当 facebook 将用户发送回该 url 时,您清除会话变量。默认的 facebook 实现在类上有destroySessionBaseFacebook(由于某种原因未在官方 sdk 文档中列出)。

于 2012-07-26T08:21:36.763 回答