Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php LinkedIn API-发布到组或配置文件共享将返回401身份验证代码_Php_Api_Linkedin - Fatal编程技术网

Php LinkedIn API-发布到组或配置文件共享将返回401身份验证代码

Php LinkedIn API-发布到组或配置文件共享将返回401身份验证代码,php,api,linkedin,Php,Api,Linkedin,我遇到了一个问题,找不到有文档记录的解决方案。现在我找到了,我把它贴在这里,以防有人遇到同样的问题 我按照步骤向LinkedIn进行身份验证并获得访问令牌,我能够毫无问题地检索我的个人资料信息和我所属的组 接下来,我想使用API向一个组发布帖子 LinkedIn API文档显示了文件获取内容的使用,但它对我不起作用。访问令牌是正确的,但我收到了401响应。提到因为我添加了ignorer\u errors=1,所以发布了小组帖子,但仍然返回了401 作为参考,这是我必须更改的代码片段,以解决401

我遇到了一个问题,找不到有文档记录的解决方案。现在我找到了,我把它贴在这里,以防有人遇到同样的问题

我按照步骤向LinkedIn进行身份验证并获得访问令牌,我能够毫无问题地检索我的个人资料信息和我所属的组

接下来,我想使用API向一个组发布帖子

LinkedIn API文档显示了
文件获取内容的使用
,但它对我不起作用。访问令牌是正确的,但我收到了
401响应
。提到因为我添加了
ignorer\u errors=1
,所以发布了小组帖子,但仍然返回了
401

作为参考,这是我必须更改的代码片段,以解决
401


解决方案概述

使用LinkedIn API发布到组,步骤如下:

设置URL:

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

$url = 'https://api.linkedin.com/v1/groups/{group_id}/posts?' . http_build_query($params);
为该职位安排职位

$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\u file\u 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>';
}
$curl=curl_init();
curl_setopt_数组($curl,数组(
CURLOPT_RETURNTRANSFER=>1,
CURLOPT_URL=>$URL,
CURLOPT_POST=>1,
CURLOPT_HTTPHEADER=>array('x-li-format:json',“内容类型:application/json”),
CURLOPT_POSTFIELDS=>$body,
));     
//在这里,我们执行代码并检查响应代码
curl_exec($curl);
$http\u status=curl\u getinfo($curl,CURLINFO\u http\u代码);
curl_close($curl);
如果($http_status==“201”){
回音日期('g:i')。“发布到LinkedIn group
”; }否则{ 回显日期('g:i')。'LinkedIn错误:'.$http_状态。'; }
我建议您使用图书馆与linkedin交谈。我根据linkedin提供的示例创建了一个PHP类,只是它使用了cURL,而且更面向对象。但那只是我的,还有很多其他的。我得到了一个LinkedIn的错误:401-有什么想法吗?最好的M@mboeckle除了使用
curl
而不是
file\u get\u content
,不,我没有。我花了很多时间在LinkedIn的支持论坛上,似乎有一些怪癖被归为代码401。如果你找不到答案,你可以在他们的支持论坛上贴一张票。祝你好运
$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>';
}