2012-09-19 24 views
1

我需要一些幫助從Facebook獲取一些JSON,基本上我需要做的就是獲取狀態文本並將其發佈到網站。獲取Facebook的JSON?

我正在使用下面的方法,但此方法的access_tokens每小時過期。

$.getJSON('https://graph.facebook.com/SOME_ID?fields=statuses.limit(10).fields(message)&access_token=SOME_ACCESS_TOKEN', function(data) {}); 

請注意,我從Facebook圖形API資源管理器得到了上面的網址。

任何其他方式來實現我想要做的?

更新:

我用FB.api,但我不知道我是否已經正確地完成它,因爲它是給我這個錯誤 「訪問令牌才能申請此資源「。

window.fbAsyncInit = function() { 
    FB.init({ 
     appId  : 'My_app_id', // App ID 
     channelUrl : '//myHost.com/facebook/channel.php', // 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('USER_ID?fields=statuses.fields(message)', function(response) { 
     console.log(response); 
    }); 
    // Additional initialization code here 
    }; 

    // Load the SDK Asynchronously 
    (function(d){ 
    var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; 
    if (d.getElementById(id)) {return;} 
    js = d.createElement('script'); js.id = id; js.async = true; 
    js.src = "//connect.facebook.net/en_US/all.js"; 
    ref.parentNode.insertBefore(js, ref); 
    }(document)); 

請注意,我用正確的值替換了(my_app_id,myHost.com和USER_ID)。

在此先感謝。

+0

如果答案是有幫助的 - 其標記爲 「接受」。這將結束這個問題。 – Artemix

回答

0

用戶需要先登錄到Facebook上,讓你的應用,然後才能調用API,更新您的代碼,以這樣的:

window.fbAsyncInit = function() { 
    FB.init({ 
     appId  : 'My_app_id', // App ID 
     channelUrl : '//myHost.com/facebook/channel.php', // Channel File 
     status  : true, // check login status 
     cookie  : true, // enable cookies to allow the server to access the session 
     xfbml  : true // parse XFBML 
    }); 

    FB.getLoginStatus(function(response) { 
     if (response.status === 'connected') { 
      FB.api('USER_ID?fields=statuses.fields(message)', function(response) { 
       console.log(response); 
      }); 
     } 
    }); 
    }; 

在getLoginStatus狀態的更多信息:http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/

您還需要同時詢問user_status的權限才能看到doc以獲取更多詳細信息:https://developers.facebook.com/docs/authentication/permissions/

調用的FB.login時,你應該問權限:

FB.login(function(response) { 
    // handle the response 
}, {scope: 'user_status'}); 

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

+0

Thanks Gabriel , 這個解決方案是爲facebook用戶提供的,我需要的是抓住JSON並將它發佈到我的網站上,就像我們說的「我最新的facebook的狀態」一樣,就像twit三角飼料。 有意義嗎? – Rob

1

假設你已經實現了Javascript SDK,我建議使用FB.api方法。

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

+0

我使用了fb.api,但它給了我這個錯誤「」需要訪問令牌來請求這個資源。「 – Rob

+1

你在登錄時詢問了正確的權限嗎?https://developers.facebook.com/ docs/authentication/permissions /要使用Javascript登錄您的用戶,請參閱:https://developers.facebook.com/docs/reference/javascript/FB.login/ –

+0

將用戶登錄後使用FB.api方法。即不在JS SDK的初始化中 –