0

我試圖訪問Facebook API的公共Facebook頁面的帖子,但我似乎無法驗證即使所有正確的憑據。 FB.api返回:如何驗證Facebook應用使用JavaScript API訪問公共頁面feed?

{"error":{"type":"http","message":"unknown error"}} 

而且也記錄在 '訪問令牌1:1號線' 的錯誤

Uncaught ReferenceError: TRJg376YTvXfk6sMur4Mggh5YnU is not defined access_token:1 

(匿名函數)

下面是我使用的代碼:

<script src="http://connect.facebook.net/en_US/all.js"></script> 
<div id="fb-root"></div> 
<button name="my_full_name" onclick="connectToFacebook()" value="My Name" /> 

<script> 
function connectToFacebook() { 

    FB.api('https://graph.facebook.com/oauth/access_token', 
    'get', 
    {client_id:'xxxxxxxxxx', client_secret:'xxxxxxxxxx',grant_type:'client_credentials'}, //client_credentials 
     function(response) { 
      alert(JSON.stringify(response)); 
     }); 

     FB.api("/publicpage/feed", 
       function (response) { 
        console.log(response); 
       }); 

    } 
</script> 

它的網站上沒有很好的記錄!有任何想法嗎?

+0

這個端點應該只能被_server-side_ apps使用 - 因爲它需要你的應用程序祕密,而你不想在客戶端JS代碼中公開那些每個人都能找到它的東西。 – CBroe

回答

-1

是的文檔是混亂的,但你錯了,都是錯的。按照這個article進行Facebook Javascript SDK的基本設置。

在那篇文章中,它一直在使用用戶訪問令牌,但是當您想訪問頁面的公共帖子時,您可以使用應用訪問令牌。應用程序訪問令牌的好處是它永不過期。這是你最終的選擇。

使用當前用戶

function getPosts() {   

     FB.api('/thepcwizardblog/feed', { limit: 10 }, function (response) { 
      for (var i = 0, l = response.data.length; i < l; i++) { 
       var post = response.data[i]; 
       var userid = post.from.id; 

       var msg; 

       if (post.message) {       
        msg = post.message;       
       } 
       else {     
        msg = post.description;       
       } 

       console.log(userid + msg); 
      } 
     }); 
} 

記錄的訪問令牌使用的應用程序訪問令牌

function getPosts() {   

     FB.api('/thepcwizardblog/feed?access_token='+accessToken+'', { limit: 10 }, function (response) { 
      for (var i = 0, l = response.data.length; i < l; i++) { 
       var post = response.data[i]; 
       var userid = post.from.id; 

       var msg; 

       if (post.message) {       
        msg = post.message;       
       } 
       else {     
        msg = post.description;       
       } 

       console.log(userid + msg); 
      } 
     }); 
} 

你可以訪問令牌您的應用here獲取公開的信息獲取的公開信息。

[編輯]

不要使用應用程序的訪問令牌由CBroe的意見建議。

+0

-1因爲暴露你的_app_訪問令牌在客戶端代碼是一個非常愚蠢的想法。 – CBroe

+0

@CBroe它比現在公佈的'client_secret'還要好:P,我會編輯它! – ThePCWizard

+0

它沒有什麼區別 - app_id | app_secret使得有效的應用程序訪問令牌(或者我可以打到端點來獲得一個)。這些 - app_secret或應用程序訪問令牌都不能在客戶端JS代碼中做任何事情。 – CBroe

相關問題