2012-02-07 162 views
1

我無法得到某種原因,我只收到了頁面ID的頁面訪問令牌...... 這是我的代碼:無法獲取頁面訪問令牌

<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js" type="text/javascript"></script> 

</head> 
<body> 
<div id="fb-root"></div> 
<script> 
    window.fbAsyncInit = function() { 
     FB.init({ 
      appId  : 'APP-ID', 
      status  : true, 
      cookie  : true, 
      xfbml  : true, 
      oauth  : true, 
     }); 
    }; 
    (function(d){ 
     var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;} 
     js = d.createElement('script'); js.id = id; js.async = true; 
     js.src = "//connect.facebook.net/en_US/all.js"; 
     d.getElementsByTagName('head')[0].appendChild(js); 
    }(document)); 
</script> 
<div class="fb-login-button" data-scope="email,user_checkins,manage_pages">Login with Facebook</div> 

<div class="result">Start</div> 
<div class="result_app_installed">Start</div> 
<script> 
// Get access token 
$.get('https://graph.facebook.com/102561956524622?fields=access_token', function(data) { 
    alert(data); 
}); 

// Get data if user is application is installed on that page or not. 
$.get("https://graph.facebook.com/102561956524622/tabs/255792581098802", 
    function(data) { 
    alert(data); 
    }); 
</script> 
</body> 
</html> 

我所試圖做的是獲取頁面訪問令牌,然後檢查是否安裝在該頁面上的應用程序.. 我在做什麼錯誤?

回答

0

https://graph.facebook.com/102561956524622?fields=access_token
將不起作用。爲了得到一個的access_token任何粉絲頁面,您需要做的步驟說明如下:

writing on the wall on behalf of my fanpage

而不是使用$不用彷徨,最好使用Facebook的JavaScript SDK的:

https://developers.facebook.com/docs/reference/javascript/

所以對於第二個請求這將是:

FB.api('/102561956524622/tabs/255792581098802', 
function(response) { 

    alert(response); 

}); 

記住,爲了使這個FB.api叫您必須授權應用第一

+0

仍然沒有工作...我得到不同的錯誤:「錯誤校驗應用程序」 「OAuthException」這是我的代碼:http://pastebin.com/YPdeU0tM – DanR 2012-02-07 14:38:53

+1

我覺得你錯了代碼和accessToken,你不能使用代碼作爲accessToken,請參考這個文檔:https://developers.facebook。 com/docs/authentication/ – 2012-02-08 09:14:56

+0

也可以使用Javascript獲取access_token您可以使用http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/方法(如果已連接)返回access_token,如果您調試它你會看到它返回的是什麼 – 2012-02-08 09:15:43

2

試着這麼做:

FB.api('/me/accounts',function(response) {alert(response);})

頁令牌返回的數據的access_token

您需要manage_pages許可。

0

Actuelly它很容易

FB.api('/'+[PAGE_ID]+'?fields=id,likes,name,access_token', function(response) { 
    console.log(response.access_token); 
}); 

不要忘記檢查,如果用戶登錄