我正在努力与LinkedIn v2 api进行图像共享。根据the LinkedIn docs,有三个步骤。
我使用php curl通过了前两个,但我坚持第三个。第3步,使用ugcPost完成。
这是我发送到ugc端点的帖子正文:
{
"author": "urn:li:person:{id-redacted}",
"lifecycleState": "PUBLISHED",
"specificContent": {
"com.linkedin.ugc.ShareContent": {
"shareCommentary": {
"text": "hello"
},
"shareMediaCategory": "IMAGE",
"media": {
"stats": "READY",
"description": {
"text": "this is a descriptoin"
},
"media": "urn:li:digitalmediaAsset:{id-redacted}",
"title": {
"text": "this is some title text"
}
}
}
},
"visibility": {
"com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
}
}
这是我用来发送curl post请求的代码:
curl_setopt($ch, CURLOPT_URL, $ugcPostEndpoint);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $out);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json","X-Restli-Protocol-Version: 2.0.0"));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$response = curl_exec($ch);
$response_status = strval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
从curl返回:
Hostname was found in DNS cache
Trying 108.174.10.12...
Connected to api.linkedin.com (108.174.10.12) port 443 (#2)
successfully set certificate verify locations:
CAfile: none
CApath: /etc/ssl/certs
SSL connection using {redacted}
Server certificate:
subject: C=US; ST=California; L=Mountain View; O=LinkedIn Corporation; CN=tablet.linkedin.com
start date: 2018-03-30 00:00:00 GMT
expire date: 2020-04-27 12:00:00 GMT
subjectAltName: api.linkedin.com matched
issuer: C=US; O=DigiCert Inc; CN=DigiCert SHA2 Secure Server CA
SSL certificate verify ok.
POST /v2/ugcPosts?oauth2_access_token={redacted} HTTP/1.1
Host: api.linkedin.com
Accept: */*
Content-Type:application/json
X-Restli-Protocol-Version: 2.0.0
Content-Length: 734
upload completely sent off: 734 out of 734 bytes
< HTTP/1.1 500 Server Error
< X-LI-ResponseOrigin: RGW
< Content-Type: application/json
< X-RestLi-Error-Response: true
< X-Restli-Gateway-Error: false
< X-RestLi-Protocol-Version: 2.0.0
< X-Li-Fabric: prod-ltx1
< Transfer-Encoding: chunked
< Connection: keep-alive
< X-Li-Pop: prod-edc2
< X-LI-Proto: http/1.1
< X-LI-UUID: {redacted}
< Set-Cookie: {redacted}
< X-LI-Route-Key: {redacted}
<
Connection #2 to host api.linkedin.com left intact
为什么会发生这500个错误?据我所知,我正在关注所有的例子/文档。 (我知道500个错误可能是他们的问题,但根据我的经验,在处理API时500错误通常意味着卷曲请求在某种程度上是错误的。
我想出了在其他人遇到类似问题的情况下我做错了什么。
正文的媒体部分必须是数组中的第0个元素,如下所示:
"media": [
{
"status": "READY",
"description": {
"text": "this is a description"
},
"media": "urn:li:digitalmediaAsset:{id}",
"title": {
"text": "this is some title text"
}
}
]
不是这个:
"media":
{
"status": "READY",
"description": {
"text": "this is a description"
},
"media": "urn:li:digitalmediaAsset:{id}",
"title": {
"text": "this is some title text"
}
}
LinkedIn文档并没有让这个显而易见。