2013-02-20 217 views
0

我正在实施我的fb应用程序,它连接到它自己的fb fanpage。我需要获得像用户名,ID等用户信息,但当我点击我的“Facebook身份验证”链接,我最终会去一个空白的白页? 您可以在这里查看我的代码:http://codepad.org/f0Tuh63vfacebook - 如何获取用户ID与phpsdk和js sdk

error_reporting(E_ALL); 
ini_set('display_errors', true); 

require 'facebook/facebook.php'; 

    $facebook = new Facebook(array(
'appId' => '1', 
'secret' => '2', 
)); 


// 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'); 
    print_r($user_profile); 
    } catch (FacebookApiException $e) { 
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>'; 
    $user = null; 
} 
} 
else{ 
$loginUrl = $facebook->getLoginUrl(
     array('scope' => 'user_about_me,user_birthday,email,publish_actions,offline_access,user_hometown,user_location', 
       'redirect_uri' => "https://domain.net/intro.php" 
     ) 
); // end of array 

} 
?> 
<!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 { ?> 

    <a href="<?php echo $loginUrl; ?>">Facebook authenticate</a> 
    <?php } ?> 
    <div id="fb-root"></div> 
    <script> 
    window.fbAsyncInit = function() { 
     FB.init({ 
     appId: '<?php echo $facebook->getAppID() ?>', 
     cookie: true, 
     xfbml: true, 
     oauth: true, 
     status: 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> 

这也是这里http://codepad.org/f0Tuh63v 我在做什么错?

也有没有办法让FB用户ID不使用phpsdk或JS SDK?或者我必须使用这些插件?

+0

好的。所以请在问题本身中提及,你已经正确地把appid和secret的值。或者使用像****这样的东西,让每个人都明白,只是通过看。 – gaurav 2013-02-20 21:41:15

回答

0

我看不出你的代码有什么问题。但是,如果您使用的是Javascript,我可以告诉您一个获得身份验证的替代方案,然后获取您需要的信息。

我在我的按钮背后使用了下面的代码,它对我的​​Facebook应用程序进行了身份验证,然后将用户重定向到了我的网站。它为我工作。

<a class="fb-login-button" align="center" target="_blank" href="https://www.facebook.com/dialog/oauth?client_id=CLIENT_ID&response_type=token&scope=PERMISSION_1,PERMISSION_2&redirect_uri=YOUR_WEBSITE"> TEXT </a> 

然后我提取了重定向URL中返回的令牌。

var url_t; // Get the redirected URL. 
access_token = url_t.split('=')[1].split('&')[0]; 

然后使用访问令牌发送HTTP请求获取所需的数据。我使用了facebook提供的GRAPH API。例如:获取用户的名字:

var xhr = new XMLHttpRequest(); 
var f_url_new = "https://graph.facebook.com/fql?q=SELECT%20name%20FROM%20user%20WHERE%20uid%20=%20me()&access_token=" + access_token; 
xhr.open("GET", f_url_new , true); 
xhr.onreadystatechange = function() { 
    if (xhr.readyState == 4) { 
     obj1 = JSON.parse(xhr.responseText); 
     var str = obj1.data[0].name.toString(); 
     var n=str.split(" "); 
     document.getElementById("name").innerText = n[0]; 
    } 
} 
xhr.send(); 

希望它给你一些关于替代方案的想法。这不是您的问题的正确答案,但可以帮助思考过程。

相关问题