如何用Facebook Live Graph API SDK添加product_items创建RTMS流地址
解决Facebook Live Stream Graph API添加product_items失败的问题
问题描述
尝试通过Facebook Live Stream Graph API SDK创建包含product_items枚举列表的RTMS流地址,已拥有创建完成的Facebook产品ID,曾尝试传入['productId1','productId2']格式的数组但无效,当前实现代码如下:
try { $response = $this->fbLatest->post("/" . $this->pageID . "/live_videos", [ 'access_token' => $this->accessToken, 'status' => 'LIVE_NOW', 'title' => 'testtsss', 'description' => 'asdsadasdadasdadadad', 'product_items' => [ array( 'id' => "25252395334406077", 'retailer_id' => "25252395334406077", 'product_id' => "25252395334406077", 'position' => array( 'x' => 0.1, 'y' => 0.1 ), 'start_time_offset_ms' => 0, 'end_time_offset_ms' => 60000 ), ] ]); $graphNode = $response->getGraphNode(); $stream_url = $graphNode->getField('stream_url'); $streamID = $graphNode->getField('id'); dd($graphNode , $stream_url); } catch (\Facebook\Exceptions\FacebookResponseException $e) { dd($e); echo 'Graph returned an error: ' . $e->getMessage(); } catch (\Facebook\Exceptions\FacebookSDKException $e) { dd($e); echo 'Facebook SDK returned an error: ' . $e->getMessage(); }
同时遇到错误,请求正确添加产品的解决方案。
解决方案
1. 移除冗余字段
Facebook Live API的product_items对象中,只需传入product_id或retailer_id其中一个,不需要同时传递id、retailer_id、product_id三个字段,冗余字段会导致API解析失败。
2. 确认权限与产品关联
- 确保你的
access_token拥有catalog_management和pages_manage_posts权限; - 确认使用的产品ID属于当前Page关联的产品目录,未关联的产品无法被添加到直播中。
3. 修正后的代码示例
try { $response = $this->fbLatest->post("/" . $this->pageID . "/live_videos", [ 'access_token' => $this->accessToken, 'status' => 'LIVE_NOW', 'title' => 'testtsss', 'description' => 'asdsadasdadasdadadad', 'product_items' => [ // 单个产品示例 [ 'product_id' => "25252395334406077", 'position' => [ 'x' => 0.1, 'y' => 0.1 ], 'start_time_offset_ms' => 0, 'end_time_offset_ms' => 60000 ], // 可添加多个产品对象 [ 'product_id' => "另一个产品ID", 'position' => [ 'x' => 0.3, 'y' => 0.2 ], 'start_time_offset_ms' => 10000, 'end_time_offset_ms' => 70000 ] ] ]); $graphNode = $response->getGraphNode(); $stream_url = $graphNode->getField('stream_url'); $streamID = $graphNode->getField('id'); dd($graphNode , $stream_url); } catch (\Facebook\Exceptions\FacebookResponseException $e) { // 打印完整错误响应以排查问题 dd($e->getResponse(), $e->getMessage()); echo 'Graph returned an error: ' . $e->getMessage(); } catch (\Facebook\Exceptions\FacebookSDKException $e) { dd($e->getMessage()); echo 'Facebook SDK returned an error: ' . $e->getMessage(); }
4. 调试错误细节
在捕获FacebookResponseException时,输出$e->getResponse()可以获取API返回的完整错误详情,比如字段验证失败、权限不足等具体原因,帮助快速定位问题。
内容的提问来源于stack exchange,提问作者Muhammad Awais Zulifqar
相关产品推荐
相关产品推荐

