2

我正在使用 Youtube Data API 并尝试获取聊天消息,为此我必须提供lifeChatIdpart参数

我的代码

    $guzzle_client = new Client();
    $res = $guzzle_client->request('GET', 'https://www.googleapis.com/youtube/v3/liveChat/messages',
        [
           'liveChatId' => $broadcastsResponse['modelData']['snippet']['liveChatId'],
           'part' => 'id,snippet'
        ]
    );

我收到错误

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Required parameter: liveChatId",
    "locationType": "parameter",
    "location": "liveChatId"
   },
   {
    "domain": "global",
    "reason": "required",
    "message": "Required parameter: part",
    "locationType": "parameter",
    "location": "part"
   }
  ],
  "code": 400,
  "message": "Required parameter: liveChatId"
 }
}

但我确定我提供了两个必需的参数。这个 var_dump 写在 guzzle 请求之前

var_dump([
    'liveChatId' => $broadcastsResponse['modelData']['snippet']['liveChatId'],
    'part' => 'id,snippet'
]);)

返回

array(2) {
  ["liveChatId"]=>
  string(20) "Cg0KC2hRYmU3akNyaXBV"
  ["part"]=>
  string(10) "id,snippet"
}

知道为什么我会收到这样的错误吗?

4

1 回答 1

1

尝试使用queryrequest 选项将它们作为查询字符串参数传递。

$guzzle_client = new Client();
$liveChatId = $broadcastsResponse['modelData']['snippet']['liveChatId'];

$res = $guzzle_client->request('GET', 'https://www.googleapis.com/youtube/v3/liveChat/messages', [
    'query' => ['liveChatId' => $liveChatId, 'part' => 'id,snippet']
]);
于 2017-03-25T12:34:42.037 回答