[英]Javascript FB.api - undefined error
我正在嘗試為facebook api使用JS接口,這是我的第一個應用程序。 這是一個片段:
HTML:
<div id="fb-root"></div>
<script src="https://connect.facebook.net/ru_RU/all.js"></script>
<script type="text/javascript">facebook_init();</script>
JS:
function facebook_init() {
FB.init({
appId : '<MY APP ID IS HERE>',
channelUrl : '/media/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.api('/me', function(response) {
alert('Your name is ' + response.name);
});
}
channel.html:
<script src="https://connect.facebook.net/ru_RU/all.js"></script>
加載此頁面時,我有“您的名字未定義”。 但是這段代碼
FB.ui({ method: 'apprequests',
message: 'MSG',
title: 'TITLE'});
按預期工作。 請你幫助我好嗎? 謝謝!
如果您通過console.log(response)
記錄您的response
變量, 您將看到錯誤 :您將收到包含此消息的錯誤對象:
"An active access token must be used to query information about the current user."
因此,如果要獲取有關當前用戶的信息,則還必須發送訪問令牌。 要獲取有關此信息的更多信息,請查看Facebook JS SDK頁面。
在調用FB.api('/ me',...)之前,您需要確保用戶已登錄Facebook並授權了您的應用程序。
以下是一般信息: http : //developers.facebook.com/docs/guides/web/#login
要克服Undefined
問題,請使用以下代碼:
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : '<APP ID>',
status : true,
xfbml : true
});
// Additional initialization code such as adding Event Listeners goes here
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
alert("connected");
connecter=true;
FB.api('/me', function(user) {
alert(user.name);
alert(user.first_name);
alert(user.last_name);
alert(user.email);
});
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.