Tags
Asked 3 years ago
14 Jul 2021
Views 329
Pasquale

Pasquale posted

Linkedin rest api to search people with name

Linkedin rest api to search people with name
angeo

angeo
answered Mar 30 '23 00:00

an example PHP code snippet that uses cURL to search for people on LinkedIn by name:




// Set the search term
$search_term = 'John Smith';

// Set the LinkedIn API endpoint and parameters
$url = "https://api.linkedin.com/v2/people?q=".urlencode($search_term)."&projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))";
$headers = [
    "Authorization: Bearer YOUR_ACCESS_TOKEN",
    "Connection: Keep-Alive"
];

// Initialize cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the request and get the response
$response = curl_exec($ch);
curl_close($ch);

// Decode the JSON response
$data = json_decode($response, true);

// Print the results
foreach ($data['elements'] as $person) {
    echo $person['firstName'].' '.$person['lastName'].'<br>';
}

Make sure to replace YOUR_ACCESS_TOKEN with your actual access token. You can obtain an access token by following the LinkedIn OAuth 2.0 authentication flow.
jassy

jassy
answered Mar 30 '23 00:00

To search for people on LinkedIn using the REST API, you can use the following endpoint:


GET https://api.linkedin.com/v2/people?q=<SEARCH_TERM>&projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))


Replace <SEARCH_TERM> with the name or keyword you want to search for. You can also add additional parameters to filter the results, such as location, current company, or school.

To use the endpoint, you'll need to authenticate and authorize your application using OAuth 2.0. You'll also need to have the r_liteprofile and r_emailaddress permissions.

Here's an example of how to use the endpoint with the requests library in Python:




import requests

search_term = 'John Smith'
access_token = 'YOUR_ACCESS_TOKEN'

url = f'https://api.linkedin.com/v2/people?q={search_term}&projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))'
headers = {'Authorization': f'Bearer {access_token}'}

response = requests.get(url, headers=headers)
data = response.json()

# Print the results
for person in data['elements']:
    print(f"{person['firstName']} {person['lastName']}")



Make sure to replace YOUR_ACCESS_TOKEN with your actual access token. You can obtain an access token by following the LinkedIn OAuth 2.0 authentication flow.
Mitul Dabhi

Mitul Dabhi
answered Mar 30 '23 00:00

example Java code snippet that uses the Apache HttpClient library to search for people on LinkedIn by name:



 
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.fasterxml.jackson.databind.ObjectMapper;

public class LinkedInSearch {

    public static void main(String[] args) throws IOException {
        // Set the search term
        String searchTerm = "John Smith";
        String encodedSearchTerm = URLEncoder.encode(searchTerm, "UTF-8");

        // Set the LinkedIn API endpoint and parameters
        String url = "https://api.linkedin.com/v2/people?q=" + encodedSearchTerm + "&projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))";

        // Set the LinkedIn API access token
        String accessToken = "YOUR_ACCESS_TOKEN";

        // Set the HTTP headers
        String authorizationHeader = "Bearer " + accessToken;
        String connectionHeader = "Keep-Alive";

        // Create the HTTP client and request
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        httpGet.addHeader(HttpHeaders.AUTHORIZATION, authorizationHeader);
        httpGet.addHeader(HttpHeaders.CONNECTION, connectionHeader);

        // Execute the request and get the response
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();

        // Parse the JSON response
        ObjectMapper objectMapper = new ObjectMapper();
        InputStream inputStream = entity.getContent();
        LinkedInSearchResult result = objectMapper.readValue(inputStream, LinkedInSearchResult.class);

        // Print the results
        for (LinkedInPerson person : result.getElements()) {
            System.out.println(person.getFirstName() + " " + person.getLastName());
        }

        // Close the HTTP client and release the connection
        EntityUtils.consume(entity);
        httpClient.close();
    }
}

class LinkedInSearchResult {
    private LinkedInPerson[] elements;
    public LinkedInPerson[] getElements() { return elements; }
    public void setElements(LinkedInPerson[] elements) { this.elements = elements; }
}

class LinkedInPerson {
    private String firstName;
    private String lastName;
    public String getFirstName() { return firstName; }
    public void setFirstName(String firstName) { this.firstName = firstName; }
    public String getLastName() { return lastName; }
    public void setLastName(String lastName) { this.lastName = lastName; }
}

Make sure to replace YOUR_ACCESS_TOKEN with your actual access token. You can obtain an access token by following the LinkedIn OAuth 2.0 authentication flow. Also, make sure to include the Jackson Databind library in your project's classpath for JSON deserialization to work.
Post Answer