It can take up to 5 minutes before a LinkedIn API access token works correctly.
When you create a new LinkedIn access token, with new permissions (a.k.a. scopes), the old access token should be invalidated, as per this note from the API docs.
Access Token Scopes and Lifetime
Access tokens stay valid until the number of seconds indicated in the expires_in field in the API response. You can go through the OAuth flow on multiple clients (browsers or devices) and simultaneously hold multiple valid access tokens if the same scope is requested. If you request a different scope than the previously granted scope, all the previous access tokens are invalidated.
However, if the old access token was recently used to make an API call -- less than 5 minutes before the new token was created -- then the old token can get stuck. When that happens, any API calls with the new token will use the old permissions (for up to 5 minutes).
You can run this app to demonstrate the problem... Or you can just read this README (to avoid all the pain of setting up your dev enivronment) 😜
This makes development difficult. Any time you make a change to the permission scopes, new tokens may keep using the old permission (from a previous token) for several minutes, making it seem like your changes didn't make any difference. But then (after the 5 minute window passes) the new permissions will suddenly start working.
This also creates problems when upgrading the LinkedIn-Version. If the new
version requires a change in permissions, the user will need to reauthorize
their account, but that could lead to a confusing situation where they get
permission errors for up to 5 minutes, because the old token is still cached
(and used) by the API. For example: in LinkedIn-Version 202305, the minimum
permission required for the
/rest/me
endpoint was r_liteprofile, but starting with 202306 it needs to be
r_basicprofile. So, we need to have our users reauthorize their LinkedIn
account, to get a new access token with the new scope, but they could get
permission errors from the old token, if it was used in the past 5 minutes.
At the time of writing this, the removal of r_liteprofile has not been
documented in the LinkedIn API Docs, but
a member of the LinkedIn support team verified, saying...
Support for r_liteprofile was dropped from version 202306 onward, but that we are trying to get official confirmation of that decision to get documentation updated. The minimal permissions allowed for
/rest/meaccess would now ber_basicprofilegoing forward.
If don't already have one, you will need to create an app in the LinkedIn developer portal, and then:
- Go to the settings for your LinkedIn app
- Tap on the Auth tab
- Under OAuth 2.0 settings, edit the Authorized redirect URLs for your app
- Add the following redirect URL:
http://localhost:3000/confirm-linkedin - Under Application credentials, copy the Client ID and Client Secret values
On your local dev machine, create a .env file (in the root of this app) with the following info:
# .env
LINKEDIN_API_KEY=[Client ID goes here]
LINKEDIN_API_SECRET=[Client Secret goes here]
ROOT_URL=http://localhost:3000
VERBOSE_LOGGING=trueStart the server:
yarn install
yarn start # or `yarn dev` (for local development with auto-reloading)
open http://localhost:3000Notice how it takes about 5 minutes before the new token starts working...