RestFB如何订阅Webhook

huangapple go评论65阅读模式
英文:

RestFB how to subscribe to webook

问题

我正在尝试复制以下请求(我在Facebook图形资源管理器中详细说明了此请求):

POST /{page_id}/subscribed_apps?subscribed_fields=feed,mention

并附带了页面访问令牌。我从该请求获得的响应是:

{
  "success": true
}

如何使用 RestFB 发出相同的请求?到目前为止,我已经得到了以下代码:

FacebookClient client = new DefaultFacebookClient(pageAccessToken, Version.VERSION_8_0);
JsonObject result = client.publish(
    String.format("/%s/%s", pageId, "subscribed_apps"),
    JsonObject.class,
    Parameter.with("subscribed_fields", "feed,mention"));

但我得到的只是:

com.restfb.exception.FacebookOAuthException: Received Facebook error response of type OAuthException: (#100) Parameters do not match any fields that can be updated (code 100, subcode null) 'null - null'

请注意,我只提供了代码部分的翻译。

英文:

I'm trying to replicate the following request (which I elaborated with the Facebook graph explorer)

POST /{page_id}/subscribed_apps?subscribed_fields=feed,mention

with it's accompanying page access token. The response I get from that request is

{
  "success": true
}

How do I make that same request using RestFB? So far I've managed to come up with:

FacebookClient client = new DefaultFacebookClient(pageAccessToken, Version.VERSION_8_0);
JsonObject result = client.publish(
	String.format("/%s/%s", pageId, "subscribed_apps"),
	JsonObject.class,
	Parameter.with("subscribed_fields", "feed,mention"));

But all I get is:

com.restfb.exception.FacebookOAuthException: Received Facebook error response of type OAuthException: (#100) Parameters do not match any fields that can be updated (code 100, subcode null) 'null - null'

答案1

得分: 1

为了使这更加简单易读,我建议您尝试以下代码:

FacebookClient client = new DefaultFacebookClient(pageAccessToken, Version.LATEST);
GraphResponse result = client.publish("me/subscribed_apps",
     GraphResponse.class, 
     Parameter.with("subscribed_fields","feed,mention"));
if (result.isSuccess()) {
  // 进行一些操作
}

这样,您就不需要page id,也不需要String.format。页面id已经与页面访问令牌连接在一起,有时这会导致更好的工作结果。

RestFB为这种类型的请求提供了一个特殊的对象,称为GraphResponse。它是一个简单的对象,提供了一些便捷方法。只要您没有完整的对象返回,比如用户或帖子,这是一种更好的方式 RestFB如何订阅Webhook

英文:

To make this more easy and readable, I suggest you try this code:

FacebookClient client = ne DefaultFacebookClient(pageAccessToken, Version.LATEST);
GraphResponse result = client.publish("me/subscribed_apps",
     GraphResponse.class, 
     Parameter.with("subscribed_fields","feed,mention"));
if (result.isSuccess()) {
  // do some stuff
}

That way, you don't need the page id and no String.format. The page id is already connected to the page access token and sometimes this leads to better working results.

RestFB provides a special object for this kind of requests called GraphResponse. It's a simple object that provides some convenience methods. As long as you don't get a complete object back, like a user or post, this is a better way RestFB如何订阅Webhook

huangapple
  • 本文由 发表于 2020年9月16日 02:52:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63908192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定