Sample code snippets for working with LinkedIn API

In this article, we are going to learn with code samples, on how to use LinkedIn API. But before that, I will explain some things you need to know about Code Snippet, I'm explaining this because of some Junior developers that don't really know much about what Code Snippet means, and also to explain how you can create some sample Code Snippets for working with LinkedIn API.

What is a code snippet?

Code Snippets are templates that make it easier to enter repeating or reusable code patterns, such as loops or conditional-statements. This allows programmers to avoid typing by repeating code during the course of a routine.

Why do I need code snippets?

  • Code Snippet make it easier to remember specific formatting for function. Some functions can be extremely complex to remember some syntax. So instead of having to search typos and syntax errors, just let a Code Snippet keep your code consistent, and also save your time from stress.

  • Code Snippets are shareable. Take for instance, If you're working with a team on a project, a Code Snippet can help ensure you and your team members are all writing certain blocks of code in the same way, this make your code more consistent and more readable.

  • Code Snippet save us from unnecessary amounts of keystrokes. Using Code Snippets will ease your stress a lot.

What are the characteristics of a Code Snippet?

  • Name: Good, self-explanatory. A name for the snippet. This is not what is used to trigger the code snippet in the editor.

  • Prefix: This is the actual keystroke that will be used to trigger the Code Snippet in the code editor. Make sure you choose a prefix that is easy for you to remember.

  • Body: This body part is just the actual code that will be copied into the space when you call your code snippet.

  • Description: This is just detailed explanations about what the code snippet contains.

How can I use Code Snippet?

This example may vary slightly based on the text editor you are using, but the fundamentals of the steps are pretty much the same:

  • Open up the User Code Snippet Interface. Note: Here, I'm using Visual Studio Code as example. In your VS Code: Go to Menu then, after that go to Code then, go to Preferences then, go to User Snippets. OR use this short command Cmd+Shift+P and search for User Snippets.

  • Now, select the language for which you would like to create a Code Snippet. I'm using C# as example.

  • Lastly, create the basic scaffolding for a Code Snippet like the example below:

private static CodeGeneratorInformation _info = new CodeGeneratorInformation(
        displayName: "Custom Scaffolder",
        description: "This is a custom scaffolder.",
        author: "Microsoft",
        version: new Version(1, 1, 0, 0),
        id: typeof(CustomCodeGenerator).Name,
        icon: ToImageSource(Resources._TemplateIconSample),
        gestures: new[] { "Controller", "View", "Area" },
        categories: new [] {
        Categories.Common, Categories.MvcController, Categories.Others
});

The examples above are just brief to get you familiar with what code snippet means, and how to use them. So, now I'm going to explain how you can create some sample Code Snippets for working with LinkedIn API using C# programming language with it's ASP.NET framework. If you are not familiar with C# programming language you can make research by joining a tutorial school, or downloading tutorial videos on youtube. They have a lot of resources both in PDFs and Vidoes. C# is a modern, object-oriented, and type-safe programming language. It is widely used for developing desktop applications, web applications and web services, etc.

What is LinkedIn API?

LinkedIn API, also known as the REST API is the heart of all programmatic interactions with LinkedIn. It provides a simple, consistent, jobs, and the interactions and relationships between the REStful API.

How do I get LinkedIn API?

To request a LinkedIn API you have to Login to the LinkedIn Developer portal. After that, click My Apps from the top of the page and select Create App. Then, complete the app details and add your company page. But for Self-Serve, complete all the steps and then click the Create App button at the bottom of the page.

Sample Code Snippets for working with LinkedIn API

In this section, I'm going to explain with some code sample on how to login integrate LinkedIn API in ASP.Net website and allow user to login with LinkedIn account, and also get LinkedIn User Profile details like ID, Name, Profile, Picture, Email Address, etc. by using the free ASPSnippets.LinkedInAPI.

This article also explains how to create a LinkedIn Application and get the LinkedIn API Key and Secret Key on LinkedIn Developer's Site.

To create an application you need to visit the Linked Developer site as explained in the last section, by login to the site. After you Login, then click the Add New Application button.

Now, login with LinkedIn account and fetch User profile details like ID, Name, Picture, and Email in ASP.Net. When you are done with that fill the following form below, inside which the most important section is the Default Scope where you need to choose what User details you want to fetch through your application.

20220718-201219.png

Once the form is submitted, you will get the LinkedIn API Key and Secret Key as shown below:

20220718-202401.png

After that, Login with LinkedIn Account and fetch User Profile details like ID, Name, Picture and Email in ASP.Net. You can now download the free ASPSnippets LinkedIn API using this, URL or you can either add it using Add Reference or copy it inside the BIN folder of your website.

Now, let show some code samples:

HTML

In the code sample below, I have an ASP.Net Button which triggers the LinkedIn Authorization process. Once the user profile details are fetched, it will be displayed using the Label and Image controls:


<asp:Button Text="Login with LinkedIn" runat="server" OnClick="Authorize" />
<asp:Panel ID="pnlDetails" runat="server" Visible="false">
    <hr />
    <asp:Image ID="imgPicture" runat="server" /><br />
    Name:
    <asp:Label ID="lblName" runat="server" /><br />
    LinkedInId:
    <asp:Label ID="lblLinkedInId" runat="server" /><br />
    Location:
    <asp:Label ID="lblLocation" runat="server" /><br />
    EmailAddress:
    <asp:Label ID="lblEmailAddress" runat="server" /><br />
    Industry:
    <asp:Label ID="lblIndustry" runat="server" /><br />
    Headline:
    <asp:Label ID="lblHeadline" runat="server" />
</asp:Panel>

To use the code sample above, you will need to import the following namespaces in your C#:


using System.Data;
using ASPSnippets.LinkedInAPI;

Then, VB.Net:


Imports System.Data
Imports ASPSnippets.LinkedInAPI

How can I use the LinkedIn Authorization and fetching the LinkedIn User Profile details?

Firstly, you need to set the LinkedIn API Key and Secret Key to its respective properties.

Now, inside the button click event handler, the Authorize method is called which will redirect the user to the LinkedIn Website where he/she will login and also grant permissions to the application to fetch his/her profile details.

Once the user has authorized, the LinkedIn Profile details are fetched using the fetch method as a DataSet object and are displayed on the page using image and label controls.

Code Sample C#:


protected void Page_Load(object sender, EventArgs e)
{
    LinkedInConnect.APIKey = "<Your LinkedIn API Key>";
    LinkedInConnect.APISecret = "<Your LinkedIn API Secret>";
    LinkedInConnect.RedirectUrl = Request.Url.AbsoluteUri.Split('?')[0];
    if (LinkedInConnect.IsAuthorized)
    {
        pnlDetails.Visible = true;
        DataSet ds = LinkedInConnect.Fetch();
        imgPicture.ImageUrl = ds.Tables["person"].Rows[0]["picture-url"].ToString();
        lblName.Text = ds.Tables["person"].Rows[0]["first-name"].ToString();
        lblName.Text += " " + ds.Tables["person"].Rows[0]["last-name"].ToString();
        lblEmailAddress.Text = ds.Tables["person"].Rows[0]["email-address"].ToString();
        lblHeadline.Text = ds.Tables["person"].Rows[0]["headline"].ToString();
        lblIndustry.Text = ds.Tables["person"].Rows[0]["industry"].ToString();
        lblLinkedInId.Text = ds.Tables["person"].Rows[0]["id"].ToString();
        lblLocation.Text = ds.Tables["location"].Rows[0]["name"].ToString();
        imgPicture.ImageUrl = ds.Tables["person"].Rows[0]["picture-url"].ToString();
    }
}

protected void Authorize(object sender, EventArgs e)
{
    LinkedInConnect.Authorize();
}

VB.Net


Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    LinkedInConnect.APIKey = "<Your LinkedIn API Key>"
    LinkedInConnect.APISecret = "<Your LinkedIn API Secret>"
    LinkedInConnect.RedirectUrl = Request.Url.AbsoluteUri.Split("?")(0)
    If LinkedInConnect.IsAuthorized Then
        pnlDetails.Visible = True
        Dim ds As DataSet = LinkedInConnect.Fetch()
        imgPicture.ImageUrl = ds.Tables("person").Rows(0)("picture-url").ToString()
        lblName.Text = ds.Tables("person").Rows(0)("first-name").ToString()
        lblName.Text += " " + ds.Tables("person").Rows(0)("last-name").ToString()
        lblEmailAddress.Text = ds.Tables("person").Rows(0)("email-address").ToString()
        lblHeadline.Text = ds.Tables("person").Rows(0)("headline").ToString()
        lblIndustry.Text = ds.Tables("person").Rows(0)("industry").ToString()
        lblLinkedInId.Text = ds.Tables("person").Rows(0)("id").ToString()
        lblLocation.Text = ds.Tables("location").Rows(0)("name").ToString()
        imgPicture.ImageUrl = ds.Tables("person").Rows(0)("picture-url").ToString()
    End If
End Sub

Protected Sub Authorize(sender As Object, e As EventArgs)
    LinkedInConnect.Authorize()
End Sub

Now, login with LinkedIn account and fetch User profile details like ID, Name, Picture and Email in ASP.Net.

Thanks for reading...

Happy Coding!

Did you find this article valuable?

Support Saint Vandora by becoming a sponsor. Any amount is appreciated!