Post 使用来自客户端的 fb api 到 facebook 页面
Post to facebook page using fb api from client
我正在尝试 post 到用户页面。这是我取得的进展,我使用此代码从 FB.getLoginStatus 获取 userId 和 userToken,然后从 post 获取我的提要。现在我想 post 到一个页面,但我一直让你没有权限 post 到一个页面。
FB.api('/me/feed', 'post', {
message:'This is my message',
link:"http://trello.com",
name: 'This is my message',
description: 'This is my message'
},function(data) {
console.log(data);
alert(data);
});
},
{ scope: "publish_actions" });
您使用了错误的端点,您必须使用“/pageid/feed”。获取用户 ID 和令牌后,您需要通过点击 '/me/accounts' 获取他有权访问的所有页面。现在你从这里得到了你想要 post 的页面的 pageid,然后点击 '/pageid/feed' 到 post 。
这是一些代码:
FB.login(function(responses1) {
FB.api('/me/accounts', 'get',function(data) {
//you will get a list here.......//for now i am hardcoding it to the first page
var page_access_token = data.data[0].access_token;
var pageId = data.data[0].id;
//now post to the page
var target = '/'+pageId+'/feed'
FB.api(target,
'post',
{ message: "message",
link: "link",
picture: "",
caption: "",
description: "",
name: "",
access_token: page_access_token
}
,function(response) {
if (!response || response.error) {
console.log("in error");
console.log(response.error);
} else {
console.log(response);
}
});
});
}, { scope: "manage_pages,publish_pages" });
您还需要将范围更改为:
manage_pages,publish_pages
你把一些事情搞混了。需要使用 FB.login
请求权限,而不是使用 API:
FB.login(function(response) {
if (response.authResponse) {
//user is logged in
}
}, {scope: 'publish_actions', return_scopes: true});
授权后,可以使用API:
FB.api('/me/feed', 'post', {
message: 'This is my message',
link: 'http://trello.com',
name: 'This is my message',
description: 'This is my message'
}, function(data) {
console.log(data);
});
});
更多信息:http://www.devils-heaven.com/facebook-javascript-sdk-login/
这是针对用户配置文件的。发布到页面(作为页面)需要 manage_pages
和 publish_pages
权限:
FB.login(function(response) {
if (response.authResponse) {
//user is logged in
}
}, {scope: 'manage_pages,publish_pages', return_scopes: true});
您必须在授权后通过此 API 调用为您的主页获取主页令牌:/{page-id}?fields=access_token
:
FB.api('/' + pageId + '', {fields: 'access_token'}, function(response) {
console.log(response);
});
之后,您可以在对post的响应中使用Token作为Page。
FB.api('/' + pageId + '/feed', 'post', {message: 'some message', access_token: pageToken}, function(response) {
console.log(response);
});
旁注:不允许预填充消息和自动posting(登录后立即),请确保在创建任何应用程序之前阅读平台政策:https://developers.facebook.com/policy/
我正在尝试 post 到用户页面。这是我取得的进展,我使用此代码从 FB.getLoginStatus 获取 userId 和 userToken,然后从 post 获取我的提要。现在我想 post 到一个页面,但我一直让你没有权限 post 到一个页面。
FB.api('/me/feed', 'post', {
message:'This is my message',
link:"http://trello.com",
name: 'This is my message',
description: 'This is my message'
},function(data) {
console.log(data);
alert(data);
});
},
{ scope: "publish_actions" });
您使用了错误的端点,您必须使用“/pageid/feed”。获取用户 ID 和令牌后,您需要通过点击 '/me/accounts' 获取他有权访问的所有页面。现在你从这里得到了你想要 post 的页面的 pageid,然后点击 '/pageid/feed' 到 post 。 这是一些代码:
FB.login(function(responses1) {
FB.api('/me/accounts', 'get',function(data) {
//you will get a list here.......//for now i am hardcoding it to the first page
var page_access_token = data.data[0].access_token;
var pageId = data.data[0].id;
//now post to the page
var target = '/'+pageId+'/feed'
FB.api(target,
'post',
{ message: "message",
link: "link",
picture: "",
caption: "",
description: "",
name: "",
access_token: page_access_token
}
,function(response) {
if (!response || response.error) {
console.log("in error");
console.log(response.error);
} else {
console.log(response);
}
});
});
}, { scope: "manage_pages,publish_pages" });
您还需要将范围更改为:
manage_pages,publish_pages
你把一些事情搞混了。需要使用 FB.login
请求权限,而不是使用 API:
FB.login(function(response) {
if (response.authResponse) {
//user is logged in
}
}, {scope: 'publish_actions', return_scopes: true});
授权后,可以使用API:
FB.api('/me/feed', 'post', {
message: 'This is my message',
link: 'http://trello.com',
name: 'This is my message',
description: 'This is my message'
}, function(data) {
console.log(data);
});
});
更多信息:http://www.devils-heaven.com/facebook-javascript-sdk-login/
这是针对用户配置文件的。发布到页面(作为页面)需要 manage_pages
和 publish_pages
权限:
FB.login(function(response) {
if (response.authResponse) {
//user is logged in
}
}, {scope: 'manage_pages,publish_pages', return_scopes: true});
您必须在授权后通过此 API 调用为您的主页获取主页令牌:/{page-id}?fields=access_token
:
FB.api('/' + pageId + '', {fields: 'access_token'}, function(response) {
console.log(response);
});
之后,您可以在对post的响应中使用Token作为Page。
FB.api('/' + pageId + '/feed', 'post', {message: 'some message', access_token: pageToken}, function(response) {
console.log(response);
});
旁注:不允许预填充消息和自动posting(登录后立即),请确保在创建任何应用程序之前阅读平台政策:https://developers.facebook.com/policy/