Login using LinkedIn

What You'll Need

Backbench account - Sign Up, the personal account will always remain free to use.

LinkedIn Developer account - Sign Up and create an App on LinkedIn.

Prerequisites

  1. Setting up linkedin app: once you have successfully created linkedin app by filling necessary details you will be redirected to Authentication page by default where you will find your CLIENT ID and CLIENT SECRET.

  2. Check/Change Permissions: You can add your application permission by marking the checkbox under default application permissions.

  3. Setting Authorized Redirect URLs: under OAuth 2.0 enter valid redirect uri (This is the uri, where you'll be redirecting after successful sign in to linkedin) for time being, you can copy paste URL mentioned in the endpoint section of backbench with /cb and click on update button. The Oauth redirect uri will look like https://USERID-BENCHID.backbench.io/cb

To prevent fraudulent transactions during the authentication process, linkedin will only communicate with URLs that you have identified as trusted endpoints. Ensure the "OAuth 2.0 Redirect URLs" field for your application contains a valid callback URL to your server that is listening to complete your portion of the authentication workflow.

Demo

Frontend

  1. Select +, in the upper right corner of file manager to create a html file . For example, say "linkedin.html" and select CREATE or hit Enter.

  2. Copy and paste the code module from below.

  3. Replace CLIENT_ID (from LinkedIn Developer)and REDIRECT_URI (from backbench endpoint) in href link.

  4. Select save.

<!doctype html>
  <head>
  </Head>
  <body>
  <h1> Login using LinkedIn</h1><br><br>
  <a href="https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&state=987654321&scope=r_basicprofile">login using linkedin</a>
  </body>
  </html>

Backend

  1. Create a module linkedin.js for Node, linkedin for BBLANG.

  2. Replace CLIENT_ID (from LinkedIn Developer Console), Redirect_uri (from Backbench Endpoint) and Client_Secret(from LinkedIn Developer Console) in request section of function.

linkedin.js
const request = require("request");

exports.endpoint = function(req, cb) {
    request.post("https://www.linkedin.com/oauth/v2/accessToken?grant_type=authorization_code&redirect_uri=redirect_uri&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&code="+req.query.code, function(error, response, body){
        if (error) {
            cb({code: error});
        } else{
           var token= JSON.parse(response.body).access_token;
           request("https://api.linkedin.com/v1/people/~?format=json&oauth2_access_token="+token , function(error, response, body){
        if (error){
            cb({code: error}); 
        } else{ 
            cb(undefined, {access_token: token, code:JSON.parse(response.body)});
            } 
        });
           
        }
   }); 
};

Then, map this module to /cb in endpoint section. For BBLANG /cb would be mapped to linkedin:auth function and to linkedin.js in Node.js.

For Node.js App:

  1. Select +, in the upper right corner to create a Module. For example, say "package.json" and select CREATE or hit Enter.

  2. Copy and paste the code module from below.

{
    "dependencies": { 
        "request": "^2.88.0"
    }
}

A package.json file lists the packages that your project depends on. allows you to specify the versions of a package that your project can use using semantic versioning rules.

Logging in

  1. Once your application is properly configured, it's time to request an authorization code.

    https://www.linkedin.com/oauth/v2/authorization?response_type=code
    &client_id=client_id
    &redirect_uri=https%3A%2F%2Fwww.example.com%2Fauth%2Flinkedin
    &state=987654321&scope=r_basicprofile
    1. client_id - The "API Key" value generated when you registered your application.

    2. response_type - The value of this field should always be: code.

    3. scope - list of member permissions your application is requesting on behalf of the user.

  2. After the web server receives the authorization code, it can exchange the authorization code for an access token. To exchange an authorization code for an access token, send a POST request:

https://www.linkedin.com/oauth/v2/accessToken?grant_type=authorization_code
&redirect_uri=https://bench-name-demojs.beta-bench.backbench.io/linkauth
&client_id=CLIENT_ID
&client_secret=CLIENT_SECRET&code=AUTHORIZATION_CODE

User Permission

using SCOPE parameter we can get users permission for the details required for our application.

By providing valid LinkedIn credentials and clicking on the "Allow Access" button, the user is approving your application's request to access their member data and interact with LinkedIn on their behalf. This approval instructs LinkedIn to redirect the user back to the callback URL that you defined in your redirect_uri parameter.

https://developer.linkedin.com/docs/oauth2#permissions

Confirming Identity

Once you've obtained an Access Token, you can start making authenticated API requests on behalf of the user by sending request

The code samples below also show the code that you need to add to use incremental authorization:

https://api.linkedin.com/v1/people/~?format=json&oauth2_access_token=ACCESS_TOKEN

Last updated