title: Why Share on LinkedIn API return 400 Bad Request?
body: <p>I'm trying to post a LinkedIn share update via its <a href="https://developer.linkedin.com/docs/share-on-linkedin" rel="nofollow">Share on LinkedIn</a> API using a PHP library <a href="https://github.com/Happyr/LinkedIn-API-client" rel="nofollow">LinkedIn-Client API</a>. I've successfully authorized the user and I got the access token returned from it. I use the token along with this share API call. Here is my code:</p>
  
  <pre><code>$linkedInOAuth = new Happyr\LinkedIn\LinkedIn(LINKEDIN_APP_ID, LINKEDIN_APP_SECRET);
  
  if ($accessToken) { // assume that it is retrieved from session
      $linkedInOAuth-&gt;setAccessToken($accessToken);
  
      $postParams = array(
          "content" =&gt; array(
              'description' =&gt; "I'm so exciting to share a post using API."
          ),
          "visibility" =&gt; array(
              "code" =&gt; "connnections-only"
          )
      );
  
      $result = $linkedInOAuth-&gt;api(
          "v1/people/~/shares",
          array("format" =&gt; "json"),
          "POST",
          $postParams
      );
  }
  </code></pre>
  
  <p>However I got 400 bad request error returned from this API call.</p>
  
  <blockquote>
    <p>Fatal error: Uncaught GuzzleException: 400: Client error response [url] <a href="https://api.linkedin.com/v1/people/~/shares?format=json&amp;oauth2_access_token=xxxxxxx" rel="nofollow">https://api.linkedin.com/v1/people/~/shares?format=json&amp;oauth2_access_token=xxxxxxx</a> [status code] 400 [reason phrase] Bad Request thrown in ...\vendor\happyr\linkedin-api-client\src\Happyr\LinkedIn\Http\GuzzleRequest.php on line 26</p>
  </blockquote>
  
  <p>What could be the problem?</p>
  
  <h3>[UPDATE on 2015-04-30 3:00 PM UTC]</h3>
  
  <p><a href="https://github.com/Happyr/LinkedIn-API-client" rel="nofollow">LinkedIn-Client API</a> uses <a href="http://guzzle.readthedocs.org/en/latest/" rel="nofollow">Guzzle</a> internally for HTTP requests. I tried to use <code>GuzzleHttp</code> directly without using <code>Happyr\LinkedIn\LinkedIn-&gt;api()</code>, but same error and no success.</p>
  
  <pre><code>if ($accessToken) {
      $url = 'https://api.linkedin.com/v1/people/~/shares?format=json&amp;oauth2_access_token=' . $accessToken;
  
      $client = new GuzzleHttp\Client();
  
      $response = $client-&gt;post($url, array(
          'headers' =&gt; array(
              'Content-Type' =&gt; 'application/json',
              'x-li-format'  =&gt; 'json'
          ),
          'json' =&gt; array(
              'comment' =&gt; 'Check out developer.linkedin.com!',
              'content' =&gt; array(
                  'description' =&gt; 'I\'m so exciting to share a post using API.'
              ),
              'visibility' =&gt; array(
                  'code' =&gt; 'connections-only'
              )
          )
      ));
  }
  </code></pre>
  
  <blockquote>
    <p>Fatal error: Uncaught exception 'GuzzleHttp\Exception\ClientException'
    with message 'Client error response [url]
    <a href="https://api.linkedin.com/v1/people/~/shares?format=json&amp;oauth2_access_token=xxxxx" rel="nofollow">https://api.linkedin.com/v1/people/~/shares?format=json&amp;oauth2_access_token=xxxxx</a>
    [status code] 400 [reason phrase] Bad Request' in
    \vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:89 Stack
    trace: #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
    \vendor\guzzlehttp\guzzle\src\Exception\RequestException.php on line
    89</p>
  </blockquote>
  
  <h3>[UPDATE on 2015-05-05 9:40 AM UTC]</h3>
  
  <p>I copied and used both xml and json examples of <a href="https://developer.linkedin.com/docs/share-on-linkedin" rel="nofollow">Share on LinkedIn API page</a>. This time, the error changed to Internal Server Error.</p>
  
  <pre><code>if ($accessToken) {
      $format = 'xml';
      $url = 'https://api.linkedin.com/v1/people/~/shares?format='.$format.'&amp;oauth2_access_token=' . $connect-&gt;accessToken;
      $postParams = array(
          "xml" =&gt; "
              &lt;share&gt;
                  &lt;comment&gt;Check out developer.linkedin.com!&lt;/comment&gt;
                  &lt;content&gt;
                      &lt;title&gt;LinkedIn Developer Resources&lt;/title&gt;
                      &lt;description&gt;Leverage LinkedIn's APIs to maximize engagement&lt;/description&gt;
                      &lt;submitted-url&gt;https://developer.linkedin.com&lt;/submitted-url&gt;
                      &lt;submitted-image-url&gt;https://example.com/logo.png&lt;/submitted-image-url&gt;
                  &lt;/content&gt;
                  &lt;visibility&gt;
                      &lt;code&gt;anyone&lt;/code&gt;
                  &lt;/visibility&gt;
              &lt;/share&gt;",
          "json" =&gt; array(
              "comment" =&gt; "Check out developer.linkedin.com!",
              "content" =&gt; array(
                  "title" =&gt; "LinkedIn Developers Resources",
                  "description" =&gt; "Leverage LinkedIn's APIs to maximize engagement",
                  "submitted-url" =&gt; "https://developer.linkedin.com",
                  "submitted-image-url" =&gt; "https://example.com/logo.png"
              ),
              "visibility" =&gt; array(
                  "code" =&gt; "anyone"
              )
          )
      );
  
      $client = new GuzzleHttp\Client();
  
      if ($format === 'xml') {
          $response = $client-&gt;post($url, array(
              'body' =&gt; $postParams['xml']
          ));
      } else {
          $response = $client-&gt;post($url, array(
              'headers' =&gt; array(
                  'Content-Type' =&gt; 'application/json',
                  'x-li-format'  =&gt; 'json'
              ),
              'json' =&gt; $postParams['json']
          ));
      }
  }
  </code></pre>
  
  <blockquote>
    <p>Fatal error: Uncaught exception 'GuzzleHttp\Exception\ServerException'
    with message 'Server error response [url]
    <a href="https://api.linkedin.com/v1/people/~/shares?format=json&amp;oauth2_access_token=xxxx" rel="nofollow">https://api.linkedin.com/v1/people/~/shares?format=json&amp;oauth2_access_token=xxxx</a>
    [status code] 500 [reason phrase] Internal Server Error'</p>
  </blockquote>