此示例(最初來自Stack Overflow文檔)顯示瞭如何獲取Facebook訪問令牌以及如何使用令牌獲取FB源和數據來顯示您的網站:
實施例:
阿活例子可在
https://newtonjoshua.com
介紹圖形API:
的圖形API是進入和離開Facebook的平臺上的數據的主要方式。這是一個低級別的基於HTTP的API,您可以使用它來查詢數據,發佈新故事,管理廣告,上傳照片以及應用程序可能需要執行的各種其他任務。
Facebook應用程序:
https://developers.facebook.com
創建一個Facebook應用程序。你會得到一個App_Id
和App_Secret
圖形API瀏覽器:
https://developers.facebook.com/tools/explorer/{{App_Id}}
/方法= GET &路徑=我%2Ffeed &版本V2.8 =
你會得到短暫的access_token
。所以這將是我們的short_lived_access_token
。
注意:在創建訪問令牌時,請選擇所有您需要的fb字段。這將授予訪問令牌訪問這些字段的權限。
訪問令牌擴展:
https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id={{APP_ID}} & client_secret = {{App_Secret}} & fb_exchange_token = {{短命-的access_token}}
您將得到一個有效期爲2米的access_token
onths。
訪問令牌調試器:
https://developers.facebook.com/tools/debug/accesstoken?q={{}的access_token} &版本V2.8 =
您可以檢查檢查access_token
的細節。
的Facebook SDK爲JavaScript:
包括下面的JavaScript在HTML的SDK異步加載到您的網頁
<script>
(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {
return;
}
js = d.createElement(s);
js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
圖形API:
讓我們的API打電話給我們的FB ID,個人資料照片,封面照片和飼料。
<script>
window.fbAsyncInit = function() {
FB.init({
appId: '{{App_Id }}',
xfbml: true,
version: 'v2.7'
});
FB.api(
'/me',
'GET', {
fields: 'id,picture{url},cover,feed',
access_token: {{access_token}}
},
function (response) {
if (response.error) {
console.error(response.error.message);
}
if (response.picture.data.url) {
profilePic = response.picture.data.url;
}
if (response.cover.source) {
coverPic = response.cover.source;
}
if (response.feed.data) {
feeds = response.feed.data;
feeds.forEach(function (feed) {
// view each feed content
});
}
if (response.feed.paging.next) {
nextFeedPage = response.feed.paging.next;
// a request to nextFeedPage will give the next set of feeds
}
}
);
};
</script>
使用圖形API瀏覽器來設計你的查詢,應在「字段」中輸入(例如:「ID,圖片{URL},蓋,進料」)
現在,您可以獲取您的來自Facebook Graph API的facebook數據使用您的access_token。
參考https://developers.facebook.com/docs/graph-api/overview/
注:您access_token
將在第2個月後到期。之後創建一個新的access_token。
謝謝Igy;這正是我所期待的;) –