2014-02-11 39 views
1

我有一個問題沒有解決記錄,我能找到。現在,我發現它了,如果有人遇到同一問題,我會在這裏發佈它。LinkedIn API - 發佈的一組或配置文件共享返回401認證碼

我遵循的步驟與LinkedIn進行身份驗證,並得到一個訪問令牌,我能找回我的個人資料信息和我是屬於沒有任何問題的羣體。

接下來,我想發表的帖子中使用API​​的一組。

LinkedIn的API文檔顯示使用file_get_contents,但它不是爲我工作。訪問令牌是正確的,但我收到了401 response。請參閱https://developer.linkedin.com/documents/code-samples。因爲我加了ignore_errors=1,這個小組的帖子被製作了,但是還是返回了401

作爲參考,這是一段代碼,我不得不改變,以解決401

$context = stream_context_create(
    array('http' => 
    array('method' =>"POST",       
     'header'=> "Content-Type:application/json\r\n", 
     'content' => $body, 
     'ignore_errors' => '1' 
    ) 
) 
); 
$res = file_get_contents($url, false, $context); 
+0

我建議你使用庫來與linkedin交談。我基於linkedin提供的示例創建了一個PHP類,除了它使用cURL並且更加面向對象。 https://github.com/EJTH/SLinkedIn/blob/master/examples/share.php但那只是我的,還有很多其他的。 – EJTH

回答

2

解決方案概述

使用LinkedIn API張貼到組,步驟是:

設置的網址:

$params = array('oauth2_access_token' => YOUR_ACCESS_TOKEN); 

$url = 'https://api.linkedin.com/v1/groups/{group_id}/posts?' . http_build_query($params); 

定身的POST

$bodyArray = array(
    'title' => $title, 
    'summary' => $userMessage, 
    'content' => array(
    'title' => '$title2', 
    'submitted-image-url' => $pictureUrl,     
    'submitted-url' => $redirectUrl, 
    'description' => $userMessage     
)    
); 
$body = json_encode($bodyArray); 

使用CURL代替get_file_contents這正是我需要的改變得到它的工作。

$curl = curl_init(); 
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1, 
    CURLOPT_URL => $url, 
    CURLOPT_POST => 1, 
    CURLOPT_HTTPHEADER => array('x-li-format: json', "Content-Type: application/json"), 
    CURLOPT_POSTFIELDS => $body, 
));  

// here we execute the code and check for response code 
curl_exec($curl); 
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
curl_close($curl); 
if ($http_status == "201"){ 
    echo date('g:i') . ' Posted to LinkedIn group <br>'; 
}else{ 
    echo date('g:i') . '<b>LinkedIn error: ' . $http_status . '</b><br>'; 
} 
+0

我得到一個LinkedIn錯誤:401 - 任何想法?最好M – mboeckle

+0

@mboeckle除了使用'curl'而不是'file_get_content'外,不,我不知道。我花了很多時間在LinkedIn的支持論壇,它似乎有一些得到集中到代碼401您可以隨時發佈自己的支持論壇的門票,如果你不能找到答案的幾個怪癖。祝你好運。 – limeygent