Facebook Meta可恢复上传API第二步报错:不支持POST请求
问题:Facebook可恢复上传API第二步初始化错误(错误码100/子码33)
我正在使用Facebook的Resumable Upload API上传媒体文件,但在第二步(初始化上传)阶段遇到如下错误:
[ "error" => [ "message" => "Unsupported post request. Object with ID 'upload:MTphdHRhY2htZW50OjY1NGY1OTNlLTVjNzMtNDkwNy05YmJhLWM4Y2M3MjU0ZjRkMT9maWxlX2xlbmd0aD00NTE0NzkmZmlsZV90eXBlPWltYWdlJTJGanBlZw==?sig=ARYbfnegf0MmYhwi1jk' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api", "type" => "GraphMethodException", "code" => 100, "error_subcode" => 33, "fbtrace_id" => "A2ESWA2-36zZ3cbJ0A1W7XZ", ], ]
我的Laravel HTTP Client实现代码如下:
$file = new Symfony\Component\HttpFoundation\File\File($filePath); $url = "https://graph.facebook.com/v18.0/1849***********/uploads?file_length={$file->getSize()}&file_type={$file->getMimeType()}"; // 示例URL:https://graph.facebook.com/v18.0/1849***********/uploads?file_length=45455&file_type=image/jepg $session = Http::acceptJson() ->contentType("application/json") ->withToken($USER_TOKEN) ->post($url) ->json(); $encoded = urlencode($session["id"]); $url = "https://graph.facebook.com/v18.0/{$encoded}"; $response = Http::acceptJson() ->attach('file', $file->getContent(), $file->getFilename()) ->withToken($USER_TOKEN, "OAuth") ->withHeaders([ 'Content-Type'=> 'multipart/form-data', "file_offset" => 0 ]) ->post($url);
错误原因排查及解决方法
核心原因1:上传会话ID被错误编码
初始化上传返回的session["id"]已经包含URL安全的编码内容(比如末尾的?sig=...签名参数),二次urlencode会破坏ID结构,导致Facebook无法识别该上传会话。
解决方法:直接使用原始会话ID拼接URL,去掉urlencode操作:
$url = "https://graph.facebook.com/v18.0/" . $session["id"];
核心原因2:请求头设置冲突
- Laravel的
attach()方法会自动将请求的Content-Type设置为multipart/form-data,手动重复设置会导致头信息异常。 acceptJson()用于声明接受JSON响应,但文件上传请求的响应格式并非JSON,该设置属于冗余且可能引发冲突。withToken()默认使用OAuth认证,无需额外指定"OAuth"参数。
解决方法:调整请求代码,移除冲突设置:
$response = Http::withToken($USER_TOKEN) ->attach('file', $file->getContent(), $file->getFilename()) ->withHeaders([ "file_offset" => 0 ]) ->post($url);
其他可能原因及解决
权限缺失:
- 确认用户令牌(
USER_TOKEN)包含目标资源的上传权限:比如上传到Facebook页面需要pages_manage_posts,上传到个人账号需要对应版本的发布权限(如publish_posts),同时需确保令牌未过期、未被撤销。 - 可通过Facebook开发者工具的令牌调试器验证权限完整性。
- 确认用户令牌(
MIME类型拼写错误:
- 示例URL中出现
image/jepg的错误拼写,正确的MIME类型应为image/jpeg。若实际代码中存在该错误,会导致初始化会话的参数无效,后续上传无法匹配到正确会话。 - 确保
$file->getMimeType()返回正确的MIME类型,可手动打印验证。
- 示例URL中出现
内容的提问来源于stack exchange,提问作者Adnan Siddiq
相关产品推荐
相关产品推荐

