linkedin - 为什么在将照片上传到 LinkedIn API 时收到 CLIENT_ERROR?
问题描述
我正在使用 LinkedIn v2 api 制作图像共享。根据LinkedIn 文档,分为三个步骤。
- 注册要上传的图像。
- 将您的图片上传到 LinkedIn。
- 创建图像共享。
完成第 2 步后,我使用 /v2/assets/{asset-id} 检查上传状态并获得“CLIENT_ERROR”。我不知道这意味着什么,也没有在 LinkedIn 文档或在线上找到太多相关信息。正如LinkedIn所要求的那样,它可能与上传二进制图像文件有关,但据我所知,我正在上传一个。
编辑:我用来上传图片的 php-curl 如下。$uploadUrl 是从图像寄存器中获取的(步骤 1。)
$data = [
'file' => curl_file_create($file, $mimeType)//;
];
ob_start();
$out = fopen('php://output', 'w');
$ch = curl_init($uploadUrl);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $out);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch,CURLOPT_USERAGENT,'curl/7.35.0');
$authorizationHeader = trim("Authorization: Bearer $accessToken");
curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorizationHeader,"Content-Type: {$mimeType}","X-Restli-Protocol-Version: 2.0.0"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($ch, CURLOPT_UPLOAD, '1L');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
fclose($out);
$debug = ob_get_clean();
print_r($debug);
$resp_obj = json_decode($response);
print_r($response);
curl_close($ch);
资产 API 正在返回:
> GET /v2/assets/{redacted} HTTP/1.1
User-Agent: curl/7.35.0
Host: api.linkedin.com
Accept: */*
Authorization: Bearer {redacted}
< HTTP/1.1 200 OK
< X-LI-ResponseOrigin: RGW
< Content-Type: application/json
< X-RestLi-Protocol-Version: 1.0.0
< Content-Length: 319
< Date: Wed, 20 Mar 2019 14:09:18 GMT
< X-Li-Fabric: prod-ltx1
< Connection: keep-alive
< X-Li-Pop: prod-edc2-nkernB
< X-LI-Proto: http/1.1
< X-LI-UUID: {redacted}
< Set-Cookie: {redacted}
< X-LI-Route-Key: {redacted}
响应对象是:
(
[serviceRelationships] => Array
(
[0] => stdClass Object
(
[identifier] => urn:li:userGeneratedContent
[relationshipType] => OWNER
)
)
[recipes] => Array
(
[0] => stdClass Object
(
[recipe] => urn:li:digitalmediaRecipe:feedshare-image
[status] => CLIENT_ERROR
)
)
[mediaTypeFamily] => STILLIMAGE
[created] => 1553090957146
[lastModified] => 1553090958505
[id] => {redacted}
[status] => ALLOWED
)
更新:当我使用命令行 curl 上传图像时它工作正常:
curl -i --upload-file {file} --header "Authorization: Bearer {auth}" {url}
更新:解决方案:
使用 file_get_contents: curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents({path-to-your-image));**
$ch = curl_init($uploadUrl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch,CURLOPT_USERAGENT,'curl/7.35.0');
$authorizationHeader = trim("Authorization: Bearer $accessToken");
curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorizationHeader,"Content-Type: {$mimeType}","X-Restli-Protocol-Version: 2.0.0"));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents({path-to-your-image));
$response = curl_exec($ch);
解决方案
将标题添加到您的代码中Content-Type:application/octet-stream
推荐阅读
- php - 如果我在简码中使用 get_template_part,则无法编辑页面
- php - Laravel 5.6 中用于排队电子邮件的动态 SMTP 数据?
- python - 无法使用部署到 Heroku 的 django 应用程序访问 postgreSQL 中的 object.id(内部服务器错误 500)
- c - 将字符串从 argv 转换为无符号字节数组
- python - PySpark - 从数据框创建多个 json 文件
- ios - 基于滚动视图在滚动时缩小导航栏
- java - java中锁的新类与新对象
- laravel - 模拟从 Facade 本身调用方法的 Laravel Facade
- java - 如何在 FirestoreRecyclerAdapter 中实现 onClick 打开一个新的 Fragment?
- swift - Swift:使用 AVPlayer 播放音频 - 音频未播放,听不到音频