为什么分享LinkedIn API返回400 Bad Request?

时间:2015-04-29 04:52:39

标签: php api linkedin

我尝试使用PHP库Share on LinkedIn通过其LinkedIn-Client API 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 [原因短语]错误请求在... \ vendor \ happyr \ linkedin-api-client \ src中引发第26行的Happyr \ LinkedIn \ Http \ GuzzleRequest.php

可能是什么问题?

[更新于2015-04-30 3:00 PM UTC]

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'
            )
        )
    ));
}
  

致命错误:未捕获的异常' GuzzleHttp \ Exception \ ClientException'   消息'客户端错误响应[url]   https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token=xxxxx   [状态代码] 400 [原因短语]错误请求'在   \ vendor \ guzzlehttp \ guzzle \ src \ Exception \ RequestException.php:89 Stack   跟踪:#0 \ vendor \ guzzlehttp \ guzzle \ src \ Subscriber \ HttpError.php(33):   GuzzleHttp \异常\ RequestException ::创建(对象(GuzzleHttp \消息\请求),   对象(GuzzleHttp \ Message \ Response))#1   \厂商\ guzzlehttp \狂饮\ SRC \事件\ Emitter.php(109):   GuzzleHttp \订户\ HttpError->的onComplete(对象(GuzzleHttp \事件\ CompleteEvent),   '完成')#2 \ vendor \ guzzlehttp \ guzzle \ src \ RequestFsm.php(91):   GuzzleHttp \ Event \ Emitter-> emit(' complete',Object(Guz in   在线上\ vendor \ guzzlehttp \ guzzle \ src \ Exception \ RequestException.php   89

[2015年5月5日上午9:40 UTC更新]

我复制并使用了Share on LinkedIn API page的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']
        ));
    }
}
  

致命错误:未捕获的异常&#39; GuzzleHttp \ Exception \ ServerException&#39;   消息&#39;服务器错误响应[url]   https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token=xxxx   [状态代码] 500 [原因短语]内部服务器错误&#39;

3 个答案:

答案 0 :(得分:2)

根据the Github issue of Happyr\LinkedIn-API-client0.5.0已经进行了一些更新,它解决了我的问题。但是,LinkedIn有关其Share API的文档不清楚。必须注意以下信息:

  1. func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){ println("Here is your selected indexPath \(indexPath.row)") tableView.deselectRowAtIndexPath(indexPath, animated: true) } 字段是共享更新内容。名称comment导致混淆。
  2. 使用字段comment,URL在共享更新内容中是可选的。
  3. comment字段表示您要共享的网址内容的快照。为了更清楚地描述,它反映了开放图元标记;
    • content会覆盖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
  4. 使用<meta property="og:image" content="..." />字段时,需要字段content。其余的是可选的。如果缺少, 400 Bad Request 将返回。
  5. LinkedIn Share API示例代码中包含的网址会导致 500内部服务器错误。它们不应用于测试目的。
  6. 以下是使用Happyr\LinkedIn-API-client的API的正确用法。

    (1)Happyr \ LinkedIn - 使用字段submitted-url

    comment

    (2)Happyr \ LinkedIn - 使用字段$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

    以下是使用GuzzleHttp的API的正确用法。

    (3)GuzzleHttp - 使用字段$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);

    comment

    (4)GuzzleHttp - 使用字段// 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也可以一起使用。

答案 1 :(得分:1)

这里只是一个快速的猜测但是你可能需要在将它发送到API之前将json_encode添加到你的$ postParams中。

$result = $linkedInOAuth->api(
    "v1/people/~/shares",
    array("format" => "json"),
    "POST",
    json_encode($postParams),
);

可选地

如果这不起作用,我还注意到Linkedin Docs说的如下。你可以尝试添加这两个标题(如果你还没有)。

  

默认情况下,所有API调用都希望以XML格式输入,但是如果您的应用程序更加通过JSON格式提交数据,则可以通过包含以下内容告知API他们将接收JSON格式的有效负载。调用中的两个HTTP标头值:

     

Content-Type:application / json

     

x-li-format:json

答案 2 :(得分:0)

您的JSON帖子正文需要更正。检查:

https://developer.linkedin.com/docs/share-on-linkedin

您需要遵循以下任何一种格式。

  1. 通过包含网址的评论进行分享

    {
      "comment": "Check out developer.linkedin.com! http://linkd.in/1FC2PyG",
      "visibility": {
        "code": "anyone"
      }
    }
    
  2. 与特定值分享

    {
      "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"
      }  
    }