title: LinkedIn OAuth2: "Unable to verify access token"
body: <p>It works <em>if and only if</em> the user is signed into LinkedIn at the time of making the OAuth2 request.</p>
  
  <p>If the user is not signed in then we encounter an error.</p>
  
  <p>Sequence of our actions:</p>
  
  <ul>
  <li>successfully fetch the fresh access token</li>
  <li>using the access token, post to the api endpoint <code>https://api.linkedin.com/v1/people/\~</code></li>
  </ul>
  
  <p>After this we receive a 401 with the following content:</p>
  
  <pre><code>{
    "errorCode": 0,
    "message": "Unable to verify access token",
    "requestId": "C0DUCX81SA",
    "status": 401,
    "timestamp": 1421946470523
  }
  </code></pre>
  
  <p>Sometimes, after some time passed, a retry with the same Access Token gives a 200. Sometimes not.</p>
  
  <p>If the user logs in to LinkedIn during this "401 period" then magically the previously fetched access token starts to work.</p>
  
  <p>I am at a loss how to solve this, as it seems to be an issue at LinkedIn.</p>
  
  <p>Has anybody any suggestions or did anybody see this behavior before?</p>
  
  <p>We tried cookie settings, wait periods before our request etc.</p>
  
  <p>We are adding LinkedIn OAuth2 authentication to Zotonic [1], but are now stuck with a non-working module.</p>
  
  <p>[1] <a href="http://zotonic.com/" rel="noreferrer">http://zotonic.com/</a></p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Someone referred to two discussions at LinkedIn. His reply now regretfully disappeared from the discussion below.</p>
  
  <p>These are the links:</p>
  
  <p><a href="https://developer.linkedin.com/forum/unable-verify-access-token" rel="noreferrer">https://developer.linkedin.com/forum/unable-verify-access-token</a></p>
  
  <p><a href="https://developer.linkedin.com/forum/unauthorized-invalid-or-expired-token-immediately-after-receiving-oauth2-token" rel="noreferrer">https://developer.linkedin.com/forum/unauthorized-invalid-or-expired-token-immediately-after-receiving-oauth2-token</a></p>
  
  <p>I have tried all suggestions in these discussions, to no avail.</p>
  
  <p><strong>Edit #2:</strong></p>
  
  <p>Checking the first discussion at LinkedIn shows that I am not the only one having these consistency issues. Something at LinkedIn is going wrong if the user has cleared cookies or has to sign in to LinkedIn during the OAuth "dance"  <a href="https://developer.linkedin.com/forum/unable-verify-access-token#comment-36950" rel="noreferrer">https://developer.linkedin.com/forum/unable-verify-access-token#comment-36950</a></p>
  
  <p><strong>Update</strong></p>
  
  <p>Solved, thanks to Matthijs Bierman, see answers below.</p>