Giter Club home page Giter Club logo

node-linkedin's Introduction

node-linkedin

Another Linkedin wrapper in Node.js

NPM

Why?

Good question! Because when I started to use LinkedIn API, I found couple of wrappers but they were not compatible with OAuth2.0, there contributors didn't made any recent commit from several months and I had to utilize the whole wrapper with nice helper functions as well.

So, I decided to write another wrapper. We need it! So we can also maintain it! However, pull request are always major and we'd love to see that!

Getting Started

Just like others, its simple and quick as per standard:

NPM

this will install the module and add the entry in package.json. Lets start using it!

var Linkedin = require('node-linkedin')('api', 'secret', 'callback');

If you need to specify state, pass custom state parameter.

var Linkedin = require('node-linkedin')('api', 'secret', 'callback', 'state');

Sometimes, you need to know if user is coming from mobile or web app, so you can specify multiple state params

var Linkedin = require('node-linkedin')('api', 'secret', 'callback', ['state1', 'state2']);

Before invoking any endpoint, please get the instance ready with your access token.

var linkedin = Linkedin.init('my_access_token');
// Now, you're ready to use any endpoint

Additionally, you can specify options. Currently, the only supported option is timeout, allowing you to specific a timeout (in ms) for the HTTP request. The default is 60 seconds (a value of 60000).

var linkedin = Linkedin.init('my_access_token', {
    timeout: 10000 /* 10 seconds */
});

OAuth 2.0

We regret to use 1.0 for authentication and linkedin also supports 2.0. So lets start using it. The below example is inspired from express.js but good enough to give the walkthrough.

// Using a library like `expressjs` the module will
// redirect for you simply by passing `res`.
app.get('/oauth/linkedin', function(req, res) {
    // This will ask for permisssions etc and redirect to callback url.
    Linkedin.auth.authorize(res, ['r_basicprofile', 'r_fullprofile', 'r_emailaddress', 'r_network', 'r_contactinfo', 'rw_nus', 'rw_groups', 'w_messages']);
});

// otherwise you can leave `res` out, and the module will respond with the redirect url
Linkedin.auth.authorize(['r_basicprofile', 'r_fullprofile', 'r_emailaddress', 'r_network', 'r_contactinfo', 'rw_nus', 'rw_groups', 'w_messages']);

// if you have specified custom accepted state params, don't forget to include one in authorize function.
Linkedin.auth.authorize(res, ['r_basicprofile', 'r_fullprofile', 'r_emailaddress', 'r_network', 'r_contactinfo', 'rw_nus', 'rw_groups', 'w_messages'], 'state1');

// Again, `res` is optional, you could pass `code` as the first parameter
// Be sure to pass the `state` parameter to verify no CSRF intrusion, see [step 2 here](https://developer.linkedin.com/docs/oauth2)
app.get('/oauth/linkedin/callback', function(req, res) {
    Linkedin.auth.getAccessToken(res, req.query.code, req.query.state, function(err, results) {
        if ( err )
            return console.error(err);

        /**
         * Results have something like:
         * {"expires_in":5184000,"access_token":". . . ."}
         */

        console.log(results);
        return res.redirect('/');
    });
});

However if you do not use express or any library which has redirect method available on res argument then you could make it optional and the function would return url to be executed instead and then you could use that to handle HTTP redirect from your own.

If you have multiple domains pointing to the same application, you might want to change the callback url based on the domain that is making the request.

app.get('/oauth/linkedin', function(req, res) {
    //Change callback url
    Linkedin.setCallback(req.protocol + '://' + req.headers.host + '/oauth/linkedin/callback');
    Linkedin.auth.authorize(res, ['r_basicprofile', 'r_fullprofile', 'r_emailaddress', 'r_network', 'r_contactinfo', 'rw_nus', 'rw_groups', 'w_messages']);

Companies Search

Supports all the calls as per the documentation available at LinkedIn Companies Search API

linkedin.companies_search.name('facebook', 1, function(err, company) {
    name = company.companies.values[0].name;
    desc = company.companies.values[0].description;
    industry = company.companies.values[0].industries.values[0].name;
    city = company.companies.values[0].locations.values[0].address.city;
    websiteUrl = company.companies.values[0].websiteUrl;
});

Companies

Supports all the calls as per the documentation available at: LinkedIn Companies API.

linkedin.companies.company('162479', function(err, company) {
    // Here you go
});

linkedin.companies.name('logica', function(err, company) {
    // Here you go
});

linkedin.companies.email_domain('apple.com', function(err, company) {
    // Here you go
});

linkedin.companies.multiple('162479,universal-name=linkedin', function(err, companies) {
    // Here you go
});

linkedin.companies.asAdmin(function(err, companies) {
    // Here you go
});

Profile

Searches for the profiles as per the criteria.

Logged In User Profile.

linkedin.people.me(function(err, $in) {
    // Loads the profile of access token owner.
});

OR

linkedin.people.me(['id', 'first-name', 'last-name'], function(err, $in) {
    // Loads the profile of access token owner.
});

Profile by Public URL.

linkedin.people.url('long_public_url_here', function(err, $in) {
    // Returns dob, education
});

OR

linkedin.people.url('long_public_url_here', ['id', 'first-name', 'last-name'], function(err, $in) {
    // Returns dob, education
});

Profile by Id.

linkedin.people.id('linkedin_id', function(err, $in) {
    // Loads the profile by id.
});

OR

linkedin.people.id('linkedin_id', ['id', 'first-name', 'last-name'], function(err, $in) {
    // Loads the profile by id.
});

Connections

Invokes LinkedIn's Connections API.

linkedin.connections.retrieve(function(err, connections) {
    // Here you go! Got your connections!
});

Groups

Implements wrapper for LinkedIn Group API and provides interface to invoke API endpoints.

PS: For now, we just have feeds available.

Group discussions by Group ID

linkedin.group.feeds(3769732, function(err, data) {
    // data: variable is ready to use.
});

OR If you want to have custom field selector, take a look at this;

linkedin.group.feeds(3769732, ['field', 'field2', 'field3'] , function(err, data) {
    // data: variable is ready to use.
});

OR even if you want to have custom sorting parameters, you can just pass them as third argument:

linkedin.group.feeds(3769732, ['field', 'field2', 'field3'], {order: 'popularity'}, function(err, data) {
    // data: variable is ready to use.
});

Author

This wrapper has been written & currently under maintenance by Hamza Waqas. He's using twitter at: @HamzaWaqas

node-linkedin's People

Contributors

ajcstriker avatar arkeologen avatar bradwalton avatar degzcs avatar djelic avatar foxdavidj avatar glavin001 avatar hamzawaqas-10p avatar imrefazekas avatar jamuhl avatar jkatzer avatar jonamx avatar joseym avatar milasevicius avatar mistryrn avatar rootux avatar sinejoe avatar sproutly avatar ziad-saab avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.