Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing an exemple #62

Closed
medlot opened this issue Apr 1, 2016 · 8 comments
Closed

Implementing an exemple #62

medlot opened this issue Apr 1, 2016 · 8 comments

Comments

@medlot
Copy link

medlot commented Apr 1, 2016

Hello,

I'm trying to implement an exemple of connexion but I always get this error:

{ errorCode: 0, message: 'Invalid access token.', requestId: 'IN32C163DK', status: 401, timestamp: 1459554255669 }

Here the code:

`var nodemailer = require('nodemailer');

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

router.get('/linkedin', function(req, res) {
console.log("hello linkedin");
var linkedin = Linkedin.init('my_access_token');
linkedin.companies_search.name('facebook', 1, function(err, company) {
console.log(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;
});
});
`

Can you help me on that please?

Thanks

@ArkeologeN
Copy link
Owner

You have to put access token here:
var linkedin = Linkedin.init('my_access_token');

Also, the object initiation should be moved on top next to loading.

var Linkedin = require('node-linkedin')('app-id', 'secret', 'callback');
var linkedin = LinkedIn.init(accessToken);

@medlot
Copy link
Author

medlot commented Apr 4, 2016

thank you for response. I have changed the code to the below and and I have this issue:

{ errorCode: 0, message: 'Invalid access token.', requestId: 'UEF0L5RK62', status: 401, timestamp: 1459766057371 }

here my code:

`
var Linkedin = require('node-linkedin')('app-id', 'secret', 'callback');

router.get('/linkedin1', function(req, res) {

Linkedin.setCallback('http://localhost:3000/linkedin');
var scope = ['r_basicprofile','rw_company_admin','w_share','r_emailaddress'];
Linkedin.auth.authorize(res, scope, '2321334321');

});

router.get('/linkedin', function(req, res) {

Linkedin.auth.getAccessToken(res, req.query.code, req.query.state, function(err, results) {
    if ( err )
        return console.error(err);
});
var linkedin = Linkedin.init(req.query.code);
linkedin.companies_search.name('facebook', 1, function(err, company) {
console.log(company);
});

});
`

Thank you for your help!

@ArkeologeN
Copy link
Owner

You need to change this line:
var linkedin = Linkedin.init(req.query.code);
into:
var linkedin = Linkedin.init(results.access_token || results.accessToken);

And let me know if it doesn't work out.

@ralyodio
Copy link

can you provide an example w/o the express garbage? I want to use node.js simple script to access linkedin api. Is it possible? I don't see a way to get access token.

@ArkeologeN
Copy link
Owner

@chovy to get access token, the following flow works:

var Linkedin = require('node-linkedin')('app-id', 'secret', '/oauth/linkedin/callback');
// express route.
// Again, `res` is optional, you could pass `code` as the first parameter
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('/');
    });
});
app.get('/oauth/linkedin', function(req, res) {
    // This will ask for permisssions etc and redirect to callback url.
    var scope = ['r_basicprofile', 'r_fullprofile', 'r_emailaddress', 'r_network', 'r_contactinfo', 'rw_nus', 'rw_groups', 'w_messages'];
    Linkedin.auth.authorize(res, scope); // scope will be an array of scopes from linkedin
});

Goto /oauth/linkedin in browser (on port where express app is running) and follow through. You will be able to see access token in console.

@ralyodio
Copy link

most oAuth services support authentication with username/password. Does this one?

I cannot open a browser, like I said its a cli script that runs on the server.

I've been able to do this with other oAuth apis, youtube, reddit, bing, twitter. but not linked in.

Here's SO answer that we are looking for answer on. Would you mind taking a look?

https://stackoverflow.com/questions/40149522/get-an-access-token-to-linkedin-api-using-email-and-password/41908860#41908860

@ArkeologeN
Copy link
Owner

@chovy I'm not sure if LinkedIn supports password based authentication. Generally, its against the practice to seek password and that's why OAuth2.0 was designed. You can see if there's an application-token or something similar in LinkedIn API (it may work as a password).

@anthonyettinger
Copy link

I agree but Oauth 2.0 itself allows for password auth for server side scripts and also development.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants