我正在开发一个简单的节点/快递/玉网站,可以获取Facebook页面的所有公共供稿 .
我创建了一个应用程序,我得到client_id(APP_ID)和client_secret(APP_SECRET) .
我的代码工作正常,但我不知道这是否是处理这种需求的正确方法 .
这是代码:
var https = require('https'),
concat = require('concat-stream'),
async = require('async');
function FacebookPage(pageId) {
if (!(this instanceof FacebookPage))
return new FacebookPage(pageId);
this.pageId = pageId;
}
FacebookPage.prototype.getPublicFeeds = function (callback) {
var pageId = this.pageId;
async.waterfall([
function (done) {
var params = {
hostname: 'graph.facebook.com',
port: 443,
path: '/oauth/access_token?client_id=MY_CLIENT_ID&' +
'client_secret=MY_CLIENT_SECRET&grant_type=client_credentials',
method: 'GET'
};
https.get(params, function (response) {
//response is a stream so it is an EventEmitter
response.setEncoding("utf8");
//More compact
response.pipe(concat(function (data) {
done(null, data);
}));
response.on("error", done);
});
},
function (access_token, done) {
var params = {
hostname: 'graph.facebook.com',
port: 443,
path: '/v2.0/' + pageId + '/feed?' + access_token,
method: 'GET'
};
https.get(params, function (response) {
//response is a stream so it is an EventEmitter
response.setEncoding("utf8");
//More compact
response.pipe(concat(function (data) {
callback(null, JSON.parse(data));
}));
response.on("error", callback);
});
}]);
};
module.exports = FacebookPage;
编辑:感谢@Tobi我可以通过按下这里解释的access_token = app_id | app_secret来删除获取access_token的部分:
1 回答
不知道你为什么工作,因为你没有交换
code
作为实际访问令牌,如果我理解正确的话)...根据https://developers.facebook.com/docs/graph-api/reference/v2.0/page/feed/,您需要
an access token ... to view publicly shared posts.
,这意味着您还可以使用app_id|app_secret
形式的应用访问令牌 .然后你可以使用
通过带有应用程序访问令牌的
access_token
参数传递 endpoints . 我还建议使用NPM模块request
或restler
,这些使得HTTP处理变得更加容易 .