[英]Why Share on LinkedIn API return 400 Bad Request?
我正在嘗試使用PHP庫LinkedIn-Client API通過其在LinkedIn API 上的共享發布LinkedIn共享更新。 我已經成功授權了用戶,並且獲得了返回的訪問令牌。 我將令牌與此共享API調用一起使用。 這是我的代碼:
$linkedInOAuth = new Happyr\LinkedIn\LinkedIn(LINKEDIN_APP_ID, LINKEDIN_APP_SECRET);
if ($accessToken) { // assume that it is retrieved from session
$linkedInOAuth->setAccessToken($accessToken);
$postParams = array(
"content" => array(
'description' => "I'm so exciting to share a post using API."
),
"visibility" => array(
"code" => "connnections-only"
)
);
$result = $linkedInOAuth->api(
"v1/people/~/shares",
array("format" => "json"),
"POST",
$postParams
);
}
但是,此API調用返回了400錯誤的請求錯誤。
致命錯誤:未被捕獲的GuzzleException:400:客戶端錯誤響應[url] https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token=xxxxxxx [狀態碼] 400 [原因短語]錯誤的請求被拋出... \\供應商\\ happyr \\ linkedin-api-client \\ src \\ Happyr \\ LinkedIn \\ Http \\ GuzzleRequest.php,第26行
可能是什么問題呢?
LinkedIn-Client API 在內部將Guzzle用於HTTP請求。 我試圖用GuzzleHttp
上,無需使用Happyr\\LinkedIn\\LinkedIn->api()
但同樣的錯誤,並沒有成功。
if ($accessToken) {
$url = 'https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token=' . $accessToken;
$client = new GuzzleHttp\Client();
$response = $client->post($url, array(
'headers' => array(
'Content-Type' => 'application/json',
'x-li-format' => 'json'
),
'json' => array(
'comment' => 'Check out developer.linkedin.com!',
'content' => array(
'description' => 'I\'m so exciting to share a post using API.'
),
'visibility' => array(
'code' => 'connections-only'
)
)
));
}
致命錯誤:帶有消息“客戶端錯誤響應[url] https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token=xxxxx [狀態代碼] 400 [狀態代碼]的未捕獲異常'GuzzleHttp \\ Exception \\ ClientException'原因短語]“錯誤請求”,位於\\ vendor \\ guzzlehttp \\ guzzle \\ src \\ Exception \\ RequestException.php:89堆棧跟蹤:#0 \\ vendor \\ guzzlehttp \\ guzzle \\ src \\ Subscriber \\ HttpError.php(33):GuzzleHttp \\ Exception \\ RequestException :: create(Object(GuzzleHttp \\ Message \\ Request),Object(GuzzleHttp \\ Message \\ Response))#1 \\ vendor \\ guzzlehttp \\ guzzle \\ src \\ Event \\ Emitter.php(109):GuzzleHttp \\ Subscriber \\ HttpError-> onComplete(Object(GuzzleHttp \\ Event \\ CompleteEvent),'complete')#2 \\ vendor \\ guzzlehttp \\ guzzle \\ src \\ RequestFsm.php(91):GuzzleHttp \\ Event \\ Emitter-> emit('complete',Object(Guz in第89行的\\ vendor \\ guzzlehttp \\ guzzle \\ src \\ Exception \\ RequestException.php
我在LinkedIn API頁面上復制並使用了Share的 xml和json示例。 這次,錯誤更改為內部服務器錯誤。
if ($accessToken) {
$format = 'xml';
$url = 'https://api.linkedin.com/v1/people/~/shares?format='.$format.'&oauth2_access_token=' . $connect->accessToken;
$postParams = array(
"xml" => "
<share>
<comment>Check out developer.linkedin.com!</comment>
<content>
<title>LinkedIn Developer Resources</title>
<description>Leverage LinkedIn's APIs to maximize engagement</description>
<submitted-url>https://developer.linkedin.com</submitted-url>
<submitted-image-url>https://example.com/logo.png</submitted-image-url>
</content>
<visibility>
<code>anyone</code>
</visibility>
</share>",
"json" => array(
"comment" => "Check out developer.linkedin.com!",
"content" => array(
"title" => "LinkedIn Developers Resources",
"description" => "Leverage LinkedIn's APIs to maximize engagement",
"submitted-url" => "https://developer.linkedin.com",
"submitted-image-url" => "https://example.com/logo.png"
),
"visibility" => array(
"code" => "anyone"
)
)
);
$client = new GuzzleHttp\Client();
if ($format === 'xml') {
$response = $client->post($url, array(
'body' => $postParams['xml']
));
} else {
$response = $client->post($url, array(
'headers' => array(
'Content-Type' => 'application/json',
'x-li-format' => 'json'
),
'json' => $postParams['json']
));
}
}
致命錯誤:消息為“服務器錯誤響應[url] https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token=xxxx [狀態代碼] 500 [狀態代碼]的未捕獲異常'GuzzleHttp \\ Exception \\ ServerException'原因短語]內部服務器錯誤”
根據Happyr \\ LinkedIn-API-client的Github問題,在0.5.0中進行了一些更新,它解決了我的問題。 但是,LinkedIn尚未提供有關其Share API的文檔。 必須注意以下信息:
comment
字段是共享更新內容。 名稱comment
導致混亂。 comment
,URL在共享更新內容中是可選的。 content
字段表示有關您共享的URL內容的快照。 為了更清楚地描述,它反映了開放圖元標簽;
content.title
覆蓋<meta property="og:title" content="..." />
content.description
覆蓋<meta property="description" content="..." />
content.title
覆蓋<meta property="og:title" content="..." />
content.submitted-url
覆蓋<meta property="og:url" content="..." />
content.submitted-image-url
覆蓋<meta property="og:image" content="..." />
content
字段時,必填字段submitted-url
。 其余的是可選的。 如果丟失,則會返回400 Bad Request 。 以下是使用Happyr \\ LinkedIn-API-client的API的正確用法。
comment
$linkedInOAuth = new Happyr\LinkedIn\LinkedIn(LINKEDIN_APP_ID, LINKEDIN_APP_SECRET);
// retrieve $accessToken from db or session
$linkedInOAuth->setAccessToken($accessToken);
$postParams = array(
'json' => array(
"comment" => "PHPLucidFrame - The simple, lightweight and flexible web application development framework http://phplucidframe.sithukyaw.com",
"visibility" => array(
"code" => "anyone"
)
)
);
$result = $linkedInOAuth->post('v1/people/~/shares', $postParams);
content
$linkedInOAuth = new Happyr\LinkedIn\LinkedIn(LINKEDIN_APP_ID, LINKEDIN_APP_SECRET);
// retrieve $accessToken from db or session
$linkedInOAuth->setAccessToken($accessToken);
$postParams = array(
'json' => array(
"content" => array(
"title" => "PHPLucidFrame",
"description" => "The simple, lightweight and flexible web application development framework",
"submitted-url" => "http://phplucidframe.sithukyaw.com"
),
"visibility" => array(
"code" => "anyone"
)
)
);
$result = $linkedInOAuth->post('v1/people/~/shares', $postParams);
以下是使用GuzzleHttp的API的正確用法。
comment
// retrieve $accessToken from db or session
$url = 'https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token=' . $accessToken;
$client = new GuzzleHttp\Client();
$response = $client->post($url, array(
"json" => array(
"comment" => "PHPLucidFrame - The simple, lightweight and flexible web application development framework http://phplucidframe.sithukyaw.com",
"visibility" => array(
"code" => "anyone"
)
)
));
content
// retrieve $accessToken from db or session
$url = 'https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token=' . $accessToken;
$client = new GuzzleHttp\Client();
$response = $client->post($url, array(
"json" => array(
"content" => array(
"title" => "PHPLucidFrame",
"description" => "The simple, lightweight and flexible web application development framework",
"submitted-url" => "http://phplucidframe.sithukyaw.com"
),
"visibility" => array(
"code" => "anyone"
)
)
));
字段comment
和content
也可以一起使用。
這里只是一個快速的猜測,但是在將其發送到API之前,您可能需要將json_encode添加到$ postParams中。
$result = $linkedInOAuth->api(
"v1/people/~/shares",
array("format" => "json"),
"POST",
json_encode($postParams),
);
交替地
如果那行不通,我還會注意到Linkedin文檔說以下內容。 您可以嘗試添加這兩個標頭(如果尚未添加)。
默認情況下,所有API調用均期望以XML格式輸入,但是如果您的應用更方便地以JSON格式提交數據,則可以通過包含以下兩個HTTP標頭來通知API他們將接收JSON格式的負載通話中的值:
內容類型:application / json
x-li格式:json
您的JSON帖子正文需要更正。 校驗:
https://developer.linkedin.com/docs/share-on-linkedin
您需要遵循以下任一格式。
通過包含網址的評論共享
{ "comment": "Check out developer.linkedin.com! http://linkd.in/1FC2PyG", "visibility": { "code": "anyone" } }
分享特定的價值
{ "comment": "Check out developer.linkedin.com!", "content": { "title": "LinkedIn Developers Resources", "description": "Leverage LinkedIn's APIs to maximize engagement", "submitted-url": "https://developer.linkedin.com", "submitted-image-url": "https://example.com/logo.png" }, "visibility": { "code": "anyone" } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.