Question: What is going on with LinkedIn Access token response:
{"serviceErrorCode":65604,"message":"Empty oauth2 access token","status":401}
even when I pass in the access token genareted manually.
Introduction
LinkedIn is one of the most popular professional networking platforms in the world, with over 722 million members as of 2021. One of the key features of LinkedIn is its API, which allows developers to build applications that can interact with LinkedIn data and functionality. However, when using the LinkedIn API, it's common to encounter errors, including the "401 Unauthorized" error. In this blog post, we will explore what this error means, how it can occur, and how to troubleshoot and fix it.
What is a 401 Unauthorized Error?
A 401 Unauthorized error is an HTTP status code that indicates that the request was made with an invalid or expired access token. An access token is a unique identifier that is issued by LinkedIn's OAuth2 authentication server and is used to authenticate requests to the LinkedIn API. When a user logs in to the LinkedIn website, they are redirected to the OAuth2 authorization endpoint, where they grant permission for the application to access their data. The OAuth2 server then issues an access token, which the application can use to make requests to the LinkedIn API on behalf of the user.
However, if the access token is invalid or expired, the LinkedIn API will return a 401 Unauthorized error. This can happen for a variety of reasons, including
* The user has revoked their permission for the application to access their data
* The access token has expired
* The access token has been tampered with or modified in some way
* The OAuth2 server is experiencing issues or is down
Troubleshooting a 401 Unauthorized Error
If you are encountering a 401 Unauthorized error when using the LinkedIn API, there are several steps you can take to troubleshoot and fix the issue.
1. Check your access token
The first step is to check your access token to ensure that it is valid and has not expired. You can do this by making a request to the LinkedIn OAuth2 introspection endpoint, which will return information about the access token, including its expiration time. If the access token has expired, you will need to obtain a new one by redirecting the user to the OAuth2 authorization endpoint and having them grant permission for the application to access their data again.
2. Check your permissions
Another possibility is that the user has revoked their permission for the application to access their data. In this case, you will need to prompt the user to grant permission again or obtain a new access token from a different user with the necessary permissions.
3. Check your code
If you are using a custom authentication flow, it's possible that there is an issue with your code. Make sure that you are following the correct OAuth2 flow and that your code is handling errors correctly. You may also want to check your logs for any error messages or stack traces that could help diagnose the issue.
4. Check LinkedIn's status
If none of the above steps work, it's possible that there is an issue with LinkedIn's API or OAuth2 server. In this case, you can check LinkedIn's status page or contact their support team for assistance.
Code Example
Here is an example of how to obtain a new access token in Python using the requests library
pythonimport requests
import json
# Set the authorization endpoint URL
auth_url = "https
//www.linkedin.com/oauth/v2/authorize"
# Set the client ID and secret
client_id = ""
client_secret = ""
# Set the redirect URI
redirect_uri = ""
# Set the scopes
scopes = ["public"]
# Set the state parameter (optional)
state = "1234"
# Encode the parameters
params = {
"response_type"
"code",
"client_id"
client_id,
"redirect_uri"
redirect_uri,
"scope"
"+".join(scopes),
"state"
state
}
# Make the authorization request
response = requests.get(auth_url, params=params)
# Check the response status code
if response.status_code == 200
# Parse the response JSON
data = json.loads(response.text)
# Extract the authorization code and state
auth_code = data["code"]
state = data["state"]
# Make the token request
token_url = "https
//www.linkedin.com/oauth/v2/token"
token_data = {
"grant_type"
"authorization_code",
"client_id"
client_id,
"client_secret"
client_secret,
"redirect_uri"
redirect_uri,
"code"
auth_code,
"state"
state
}
# Make the token request
token_response = requests.post(token_url, data=token_data)
# Check the response status code
if token_response.status_code == 200
# Parse the response JSON
token_data = json.loads(token_response.text)
# Extract the access token and refresh token
access_token = token_data["access_token"]
refresh_token = token_data["refresh_token"]
# Use the access token to make API requests
# ...
else
# Handle the error
print("Error obtaining token
", token_response.text)
else
# Handle the error
print("Error authorizing user
", response.text)
Conclusion
In conclusion, a 401 Unauthorized error when using the LinkedIn API can occur for a variety of reasons, including invalid or expired access tokens, revoked permissions, and issues with the OAuth2 server. To troubleshoot and fix this issue, you should check your access token, permissions, code, and LinkedIn's status, and follow the correct OAuth2 flow. By following these steps
I got this error as well "{"error":"access_denied","error_description":"This application is not allowed to create application tokens"}"
Luke said:
LinkedIn API is a pain to deal with. Am now getting "Not enough permissions to access: GET-organization /organizationAcls"