调用LinkedIn AdAccount Users API报错:accounts参数值类型无效
解决LinkedIn API获取广告账户用户的参数类型错误
调用LinkedIn API获取广告账户用户时,返回如下400错误:
[ "message" => "Invalid value type for parameter accounts", "status" => 400 ]
使用Laravel Illuminate\Support\Facades\Http 发起请求的原代码:
$response = Http::withHeaders([ 'X-Restli-Protocol-Version' => '2.0.0', 'LinkedIn-Version' => '202304', 'Authorization' => 'Bearer xxxxxxxxx', ]) ->baseUrl('https://api.linkedin.com/rest/') ->get('adAccountUsers', [ 'q' => 'accounts', 'accounts' => 'urn:li:sponsoredAccount:509935087' ]);
问题原因
accounts 参数的类型不符合API要求:LinkedIn API规定该参数需为数组格式,即使仅传递单个账户URN,也必须用数组包裹。
修复代码
将accounts参数改为数组形式:
$response = Http::withHeaders([ 'X-Restli-Protocol-Version' => '2.0.0', 'LinkedIn-Version' => '202304', 'Authorization' => 'Bearer xxxxxxxxx', ]) ->baseUrl('https://api.linkedin.com/rest/') ->get('adAccountUsers', [ 'q' => 'accounts', 'accounts' => ['urn:li:sponsoredAccount:509935087'] ]);
Laravel的Http门面会自动将数组参数转换为符合LinkedIn RESTli 2.0.0协议的查询字符串格式,API即可正确解析参数类型。
内容的提问来源于stack exchange,提问作者Mina Zakaria
相关产品推荐
相关产品推荐

