LinkedIn评论API调用报错:路径变量语法异常求助
LinkedIn Comments API 路径变量语法异常问题排查
核心原因
你遇到的「Syntax exception in path variables」错误,本质是URL路径中的URN未做URL编码。LinkedIn API要求路径里的特殊字符(比如URN中的冒号:)必须经过编码,直接拼接未编码的URN会导致API解析路径时出现语法错误。
解决方案
在拼接URL时,对$postId进行URL编码,使用PHP的rawurlencode()函数处理即可:
$res = Http::withHeaders([ "LinkedIn-Version" => "202301", "X-Restli-Protocol-Version" => "2.0.0", ]) ->withToken($post->account->bearer_token) ->post( sprintf( env("LINKEDIN_API")."socialActions/%s/comments", rawurlencode($postId) // 对路径中的URN做URL编码 ), [ "actor" => $post->account->account_id, "object" => $postId, // 请求体里的object无需编码,保留原始URN格式即可 "message" => [ "text" => $post->linkedin_first_comment, ], ] );
验证点
编码后的请求URL路径部分应变为:socialActions/urn%3Ali%3Ashare%3A7037368272257712128/comments
额外检查
如果编码后仍报错,确认actor字段的值是否为正确的URN格式(比如urn:li:person:xxxxxx),LinkedIn API要求actor必须是代表用户或组织的标准URN。
内容的提问来源于stack exchange,提问作者Jamie
相关产品推荐
相关产品推荐

