YouTube Api:snippet.live_chat_id是必需的

时间:2017-10-08 14:57:45

标签: php api curl youtube

我一直在努力使用youtube。 问题是,我已经制作了自己的php curl脚本,它应该连接到youtube api并向我当前的流发送一条livechat消息。

我非常接近完成这个问题,但只有最后一部分才能阻止我。

我目前使用的脚本是:

$data = array("snippet" => ["type" => 'textMessageEvent', 'textMessageDetails' => ['messageText' => '<3']], 'livechatid' => '{{livechatid_here}}');
    $data_string = json_encode($data);

    $ch = curl_init('https://www.googleapis.com/youtube/v3/liveChat/messages?part=snippet&fields=authorDetails%2Ckind%2Csnippet&key={{Here is my key}}');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string),
            'Authorization: Bearer {{access_key}} ')
    );

    $result = curl_exec($ch);
    dd($result);

但我不断收到以下错误:

  "code": 400,
  "message": "snippet.live_chat_id is required"\n

所以我所做的就是改变了&#39; livechatid&#39;到:

  • liveChatId(API告诉我)
  • live_Chat_Id
  • livechatid
  • snippet.livechatid
  • snippet.liveChatId
  • snippet.live_chat_id

这些都没有奏效。 有谁知道我怎么解决这个问题?

当然,我一直在从上面的代码中删除我的密钥和access_tokens。

我也尝试将它们添加到标题中,但我仍然一次又一次地得到同样的错误。

有人知道我怎么能解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

liveChatId放入snippet字段:

$data = array("snippet" => [
  "type" => 'textMessageEvent',
  'textMessageDetails' => ['messageText' => '<3'],
  'liveChatId' => 'YOUR_LIVE_CHAT_ID'
]);
$data_string = json_encode($data);