文档已更新。
中文(简体) 译文尚未完成。
英语更新时间:2021年5月27日

与观众互动

您可以使用直播视频 API 获取观众对直播视频发出的评论和心情,以便视频制作者和主播与观众互动。为此,您可以通过定期查询 LiveVideo 对象来获取当前评论和心情,或将服务器发送事件设置为接收实时评论和心情

获取当前评论和心情

如要获取观众当前对直播视频发出的评论或心情,请发送请求至:

GET /{live-video-id}/comments
GET /{live-video-id}/reactions

请求示例

curl -i -X GET \
  "https://graph.facebook.com/{live-video-id}/comments?access_token={access-token}"
GraphRequest request = GraphRequest.newGraphPathRequest(
  accessToken,
  "/{live-video-id}/comments",
  new GraphRequest.Callback() {
    @Override
    public void onCompleted(GraphResponse response) {
      // Insert your code here
    }
});

request.executeAsync();
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
    initWithGraphPath:@"/{live-video-id}/comments"
           parameters:nil
           HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
    // Insert your code here
}];
FB.api(
  '/{live-video-id}/comments',
  'GET',
  {},
  function(response) {
      // Insert your code here
  }
);
try {
  // Returns a `FacebookFacebookResponse` object
  $response = $fb->get(
    '/{live-video-id}/comments',
    '{access-token}'
  );
} catch(FacebookExceptionsFacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(FacebookExceptionsFacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}
$graphNode = $response->getGraphNode();

响应示例

{
  "data": [
    {
      "created_time": "2018-08-30T18:47:02+0000",
      "from": {
        "name": "Steph C.",
        "id": "552524095105158"
      },
      "message": "This is such a great live stream.",
      "id": "911936075671494_911936769004758"
    },
    {
      "created_time": "2018-08-30T18:47:17+0000",
      "from": {
        "name": "Kevin D.",
        "id": "552524095105158"
      },
      "message": "Shoutout over here!",
      "id": "911936075671494_911936909004744"
    },
    {
      "created_time": "2018-08-30T18:48:14+0000",
      "from": {
        "name": "Clay T.",
        "id": "552524095105158"
      },
      "message": "Where is this place?",
      "id": "911936075671494_911937292338039"
    }
    ],
  "paging": {
    "cursors": {
      "before": "WTI5d...",
      "after": "WTI5d..."
    }
  }
}

如需有关可返回字段以及筛选和排序说明的信息,请参阅评论心情连线参考文档。您可以每隔几秒对评论和心情进行一次投票。

接收实时评论和心情

如要在浏览器客户端中实时接收评论和心情,请设置服务器发送事件,并向以下端点发送请求:

GET /{live-video-id}/live_comments
GET /{live-video-id}/live_reactions

注意,直播事件的主机网址为:https://streaming-graph.facebook.com

请求示例

GET https://streaming-graph.facebook.com/{live-video-id}/live_comments?access_token={access-token}
var source = new EventSource("https://streaming-graph.facebook.com/{live-video-id}/live_comments?access_token={access-token}");
source.onmessage = function(event) {
  // Do something with event.message for example
};

响应示例

: ping
data:
  {
    "created_time":"2018-08-30T21:11:01+0000",
    "id":"911936075671494_912014908996944",
    "view_id":43329028,
    "from":
      {
        "id":"552524095105158",
        "name":"Kerry Fisher"
      },
    "message":"I love this video!"
  }
: ping
: ping

如需有关可返回字段、ping 频率以及筛选的信息,请参阅直播评论直播心情参考文档。