【发布时间】:2014-01-17 06:15:11
【问题描述】:
我正在开发一个自定义应用程序来自动发布到我的业务 Facebook 页面。 我创建了一个新的应用配置文件并获得了 API 密钥和密钥,并且知道如何使用图形 API 自动发布到我的 Facebook 页面。
我的代码:
FB.init({ appId: "My FB API ID", status: true, cookie: true, frictionlessRequests: true });
FB.login(function (response) {
if (response.authResponse) {
var acc_token = response.authResponse.accessToken;
FB.api('/me/accounts', 'GET', '', function (response) {
console.log(response);
if (!response || response.error) {
alert(JSON.stringify(response.error));
}
else {
if (response.data.length > 0) { //-- Check for page list
acc_token = response.data[0].access_token; //== Change Accesstoken If a page is exist
}
var data = {
name: "Automatically Feed Posting",
link: "www,example,com",
access_token: acc_token,
description: "Hello, it's for testing"
}
FB.api('me/feed', 'post', data, function (response) {
if (!response || response.error) {
alert(JSON.stringify(response.error));
}
else {
alert("Your Feed Successfully Posted");
}
});
}
});
}
}, { scope: 'email,user_likes,publish_actions,publish_stream,read_stream,manage_pages,rsvp_event' });
问题是:如果用户有多个页面,并请求'/me/accounts',我会得到所有页面的列表。在这里,我想首先询问用户他想使用哪个页面来自动发布。
(在当前的高级/以上代码中,我获取页面列表的第一个索引并将其用于发布)。
更新:
在做了一些谷歌搜索之后......我发现了很多帖子,他们都建议手动保存/询问用户page's Id(在发布之前)并在获取所有页面列表并将此pageId与列出并执行进一步的操作。
例如:
var Userpage_id = "1234567890";
FB.init({ appId: "My FB API ID", status: true, cookie: true, frictionlessRequests: true });
FB.login(function (response) {
if (response.authResponse) {
var acc_token = response.authResponse.accessToken;
FB.api('/me/accounts', 'GET', '', function (response) {
console.log(response);
if (!response || response.error) {
alert(JSON.stringify(response.error));
}
else {
if (response.data.length > 0) { //-- Check If page list
for(var i=0; i<response.data.length; i++ ) {
if(response.data[i].page_id == Userpage_id) {
acc_token = response.data[0].access_token; //== Change Accesstoken If a page is exist
}
}
}
var data = {
name: "Automatically Feed Posting",
link: "www,example,com",
access_token: acc_token,
description: "Hello, it's for testing"
}
FB.api('me/feed', 'post', data, function (response) {
if (!response || response.error) {
alert(JSON.stringify(response.error));
}
else {
alert("Your Feed Successfully Posted");
}
});
}
});
}
}, { scope: 'email,user_likes,publish_actions,publish_stream,read_stream,manage_pages,rsvp_event' });
但我不喜欢这样做,有什么方法可以向用户显示所有页面列表(不要手动输入pageid)并在选择页面后执行发布?
更新:
我在 API 文档中找到了一些 "enable_profile_selector" 选项。
enable_profile_selector: prompt the user to grant permission for one or more Pages.
它有助于我实现目标吗?
任何帮助将不胜感激......!!!
【问题讨论】:
标签: facebook facebook-graph-api facebook-javascript-sdk