通过Facebook的graphics节点JS发布post

我目前正在寻找通过创build一个自定义应用程序,使我可以轻松发布我的Facebook页面(不是我的个人时间表)的状态更新自动化一些任务。 我的后端在NodeJS上运行。

我的客户端代码具有以下内容:

window.fbAsyncInit = function() { FB.init({ appId : 'xxxxxxxxxxxxx', // App ID channelUrl : '//xxxxxxxxxxx, // Channel File status : true, // check login status cookie : true, // enable cookies to allow the server to access the session xfbml : true // parse XFBML }); // Additional init code here FB.getLoginStatus(function(response) { if (response.status === 'connected') { console.log("You are signed into FB"); var access_token = FB.getAuthResponse()['accessToken']; console.log('Access Token = ' + access_token); FB.api('/me/accounts', function(response) { for(var i=0;i <response.data.length;i++){; var page = response.data[i]; if(page.id==my_id){ var page_token = page.access_token; console.log(page_token); var param = 'callback=?&username=' + GetURLParameter('username')+'&session='+ GetURLParameter('session')+'&access_token='+ page_token; $.post(saveTokenEndpoint, param, function(data) { console.log(data); }); } } }); FB.api('/me', function(response) { username = response.name; userid = response.id; $('#username').text('~Hi '+username+'!'); $(document).trigger('fbInit'); }); // connected } else if (response.status === 'not_authorized') { // not_authorized login(); } else { login(); // not_logged_in } }); }; // 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)); function login() { FB.login(function(response) { if (response.authResponse) { console.log("You are signed into FB"); var access_token = FB.getAuthResponse()['accessToken']; FB.api('/me', function(response) { username = response.name; userid = response.id; $('#username').text('~Hi '+username+'!'); $(document).trigger('fbInit'); }); FB.api('/me/accounts', function(response) { // handle response for(page in response.data){ if(page.id==my_id){ var page_token = page.access_token; console.log(page_token); var param = 'callback=?&username=' + GetURLParameter('username')+'&session='+ GetURLParameter('session')+'&access_token='+ page_token; $.post(saveTokenEndpoint, param, function(data) { console.log(data); }); } } }); } else { // cancelled } }, {scope: 'publish_stream, manage_pages, offline_access'}); } 

我的服务器端代码:

 // saves the token received for future use in posting app.post('/api/saveToken',function(req,res){ var username = req.body.username; var access_token = req.body.access_token; var session = req.body.session; user.findOne({ username: username, session: session },function(err, userObj){ if(userObj!=null){ userObj.token = access_token; console.log(access_token); userObj.save(); res.jsonp({status:'true'}); }else{ res.jsonp({status:'false'}); } }); }); 

我正在使用这个方法在我的页面上发布我的post:

 var data = querystring.stringify({ access_token: token, message: message }); var options = { host: 'graph.facebook.com', port: 443, path: '/app_name/feed', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': data.length } }; var req = https.request(options, function(res) { res.setEncoding('utf8'); res.on('data', function (chunk) { console.log("body: " + chunk); }); res.on('end', function(){ // see http nodejs documentation to see end console.log("\nfinished posting message"); conObj.approval = 'published'; conObj.save(); }); }); req.on('error', function(e) { console.error(e); }); req.write(data); req.end(); 

现在的情况:

我的代码正在工作,我实际上可以看到我的网页墙上的post。

除了我自己以外,其他任何人都不会看到。

如何让它正常出现暂时:当我直接从graphicsAPI资源pipe理器 (/我/帐户)复制令牌,我得到它的工作。 https://developers.facebook.com/tools/explorer?method=GET&path=me%2Faccounts

最终,我想要做的就是find一种方式在我的页面上发布post

我监测了通过这两个令牌所做的差异,并没有发现任何差异。 他们的范围是“公众”和“每个人”。

专家提供咨询! 我觉得这一切非常令人费解。

如果一切都失败了,我打算尝试NodeJS facebook,比如facebook-node-sdk和nodejs-facebook-sdk库。

感谢提前回复!

你应该做OAUTH服务器端,而不是客户端。

以下是使用node.js在Facebook Graph API中发布post的一个工作示例: http : //code.runnable.com/facebook/UTlPM1-f2W1TAABY

您尚未使用的唯一依赖关系是您应该使用的请求。