我想使用facebook的broadcast api为广播消息创建自定义标签
我可以按照文档中的说明创建标签然而我得到一个响应数组,而不是我的自定义标签的单个ID,这就是我要求api的方式
function createlabel(token, getlabel) {
var labels;
var options = {
name: "firstbroadcast",
uri: "https://graph.facebook.com/v2.11/me/custom_labels?",
qs: {
access_token: token
},
json: true,
}
requestpromise(options).then(function (label) {
console.log('returned label', label);
labels = label;
getlabel(label);
})
}
上述请求的响应是这样的
returned label { data: [ { id: '1721744167882031' }, { id:
'1562439310518208' } ],
paging:
{ cursors:
{ before: 'QVFIUldfeDVCVnBIOEo0Q3ZADZA2tIazNtUWxhNlpTUlhOVkQyU0NFZA1IzVGlnbGhJX2lCdWxuQUZAhYUlSaWlnZAFZAZAeU96Q3VrWGtCUHlaTnA1a0p5RmplNEh3',
after:'QVFIUm1WRGVFcXRpSGh1RGtRQzd0eTZAGczZAlNUY5SmdFYUtsMEdSdmpYZAVJhWHVCRGdCQm9mU0lHbHg3X0NqbUc5QkhTNmswMm14WHRNZA3hWeDR0QkdkMTJB' } } }
获取标签的ID后,我从返回的数据中获取第一个索引的id,并使用它将PSID与标签关联起来
function associatelabeltopsid(user, label, token) {
console.log('userid: ' + user + ' label :' + label);
var options = {
user: user,
uri: "https://graph.facebook.com/v2.11/" + label + "/label?
access_token="+token,
}
requestpromise(options).then(function (response) {
console.log('association done', response);
}).catch(function (error, response, body) {
if (error) {
console.log("sending error", error.message);
} else if (response.body.error) {
console.log("response body error senttext");
console.log(response.body.error);
}
})
}
上述函数“associatelabeltopsid”的三个参数是user = pagescope的收件人id,label = [在custom_labels端点响应返回的数组的第一个索引上的id],token = {page access token}
上述请求的响应是这样的,这使我无法将我的标签与psid相关联
sending error 400 - "{\"error\":{\"message\":\"(#100) Tried accessing nonexisting field (label) on node type (PageUserMessageThreadLabel)\",\"type\":\"OAuthException\",\"code\":100,\"fbtrace_id\":\"Esk6606yfRg\"}}"
我在这里做错了什么,为什么端点上的请求custom_labels返回标签的id数组而不是单个id,我使用哪一个,以及如何将PSID与标签相关联。
要扩展@CBroe的评论 - 您需要在请求选项中设置method: 'POST'
。如果未设置GET
,则request-promise模块将默认为method
。