我正在尝试使用Facebook Graph API从公共页面获取最新状态,让我们说http://www.facebook.com/microsoft
据http://developers.facebook.com/tools/explorer/?method=GET&path=microsoft%2Fstatuses说 - 我需要一个访问令牌。由于微软的页面是“公开的”,这是否真的如此?没有访问令牌,我无法访问这些公共状态吗?
如果是这种情况,为我的网站创建访问令牌的正确方法如何?我有一个App ID,但http://developers.facebook.com/docs/authentication/的所有示例都描述了处理用户登录。我只想在Microsoft页面上获取最新状态更新并将其显示在我的网站上。
这是设计的。一旦可以从没有访问令牌的公共页面获取最新状态。这已被更改,以阻止对API的匿名匿名访问。您可以使用图形API通过以下调用获取应用程序的访问令牌(如果您没有为您的网站设置Facebook应用程序 - 您应该创建它):
https://graph.facebook.com/oauth/access_token?
client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&
grant_type=client_credentials
这称为App Access Token。然后使用上面的app访问令牌继续进行实际的API调用。
希望这可以帮助
您可以使用AppID和密钥来获取任何页面的公开帖子/摘要。这样您就不需要获取访问令牌。如下所示。
https://graph.facebook.com/PAGE-ID/feed?access_token=APP-ID|APP-SECRET
并获得帖子。
https://graph.facebook.com/PAGE-ID/posts?access_token=APP-ID|APP-SECRET
您可以通过简单地请求浏览器请求的网站来获取帖子,然后从HTML中提取帖子。
在NodeJS中你可以这样做:
// npm install request cheerio request-promise-native
const rp = require('request-promise-native'); // requires installation of `request`
const cheerio = require('cheerio');
function GetFbPosts(pageUrl) {
const requestOptions = {
url: pageUrl,
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0'
}
};
return rp.get(requestOptions).then( postsHtml => {
const $ = cheerio.load(postsHtml);
const timeLinePostEls = $('.userContent').map((i,el)=>$(el)).get();
const posts = timeLinePostEls.map(post=>{
return {
message: post.html(),
created_time: post.parents('.userContentWrapper').find('.timestampContent').html()
}
});
return posts;
});
}
GetFbPosts('https://www.facebook.com/pg/officialstackoverflow/posts/').then(posts=>{
// Log all posts
for (const post of posts) {
console.log(post.created_at, post.message);
}
});