Don't have `r_emailaddress` or `r_liteprofile` scopes in linkedIn app API

2 min read 05-10-2024
Don't have `r_emailaddress` or `r_liteprofile` scopes in linkedIn app API


LinkedIn API: Missing r_emailaddress and r_liteprofile Scopes - A Common Issue and Solution

Problem: You're trying to access a LinkedIn API endpoint that requires either the r_emailaddress or r_liteprofile scope, but your application is not authorized to use these scopes. This can lead to errors and prevent your application from functioning as intended.

Understanding the Problem:

Imagine trying to access a friend's private information on a social media platform without their permission. That's essentially what happens when your LinkedIn app tries to access data requiring r_emailaddress or r_liteprofile scopes without authorization.

Scenario:

You're building a LinkedIn app that needs to fetch a user's email address or basic profile information. You've set up your API credentials and are trying to make a request to the GET /people/~:(id,email-address) endpoint to retrieve the email address. However, you receive an error message saying "Unauthorized" or "Insufficient permissions".

Sample Code:

import requests

# Replace with your LinkedIn API credentials
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
redirect_uri = "https://YOUR_APP_URL"

# Access token request (Simplified for illustration)
access_token_url = "https://www.linkedin.com/oauth/v2/accessToken"
access_token_params = {
    "grant_type": "authorization_code",
    "code": "YOUR_AUTHORIZATION_CODE",
    "redirect_uri": redirect_uri,
    "client_id": client_id,
    "client_secret": client_secret,
}
response = requests.post(access_token_url, data=access_token_params)

# Get access token (Simplified for illustration)
access_token = response.json()["access_token"]

# API request (Simplified for illustration)
profile_url = "https://api.linkedin.com/v2/people/~:(id,email-address)"
headers = {"Authorization": f"Bearer {access_token}"}
response = requests.get(profile_url, headers=headers)

# Handle response
if response.status_code == 200:
    profile_data = response.json()
    print(profile_data["emailAddress"])
else:
    print(f"Error: {response.text}")

Analysis:

The r_emailaddress scope grants your application permission to access a user's email address. The r_liteprofile scope allows you to retrieve basic profile information, including name, headline, and profile picture.

The error you encounter is because your application is trying to access protected information without the necessary authorization.

Solution:

  1. Register Your Application with LinkedIn: If you haven't already, create a LinkedIn application and obtain your client ID and client secret.

  2. Specify Required Scopes: When you request authorization from the user, ensure you include the r_emailaddress or r_liteprofile scope (or both, depending on your application's needs).

    Here's how to specify the r_emailaddress scope in the authorization URL:

    https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=YOUR_CLIENT_ID&redirect_uri=https://YOUR_APP_URL&scope=r_emailaddress
    
  3. Handle User Authorization: The user will be redirected to a LinkedIn login screen where they can grant your application the requested permissions.

  4. Obtain Access Token: Once the user approves the permissions, you'll receive an authorization code. Use this code to request an access token. This access token will allow your application to access the requested information.

Additional Value:

  • Transparency is Key: Clearly inform users about the data you're accessing and why you need it. Provide clear and concise explanations of the requested permissions in your application's authorization flow.
  • Respect User Privacy: Only request the scopes you truly need. Don't ask for access to information that's not essential for your application's functionality.
  • Handle Errors Gracefully: Implement error handling to gracefully manage situations where the user declines access or the API returns an error.

Resources:

By following these steps, you can avoid the "Unauthorized" error and successfully access the data you need from the LinkedIn API.