Skip to content

Instantly share code, notes, and snippets.

@S-codes14
Created October 16, 2021 19:27
Show Gist options
  • Save S-codes14/6fbb8b0198a96204a4878a21715b240e to your computer and use it in GitHub Desktop.
Save S-codes14/6fbb8b0198a96204a4878a21715b240e to your computer and use it in GitHub Desktop.
linkedin api post
const fetch = require('node-fetch');
const debug = require('debug')('linkedin-api-wrapper:core');
module.exports = function(options) {
options.version = options.version || 'v2';
return {
access_token: options.access_token,
version: options.version,
api: api,
};
}
function checkStatus(res) {
if (res.ok)
return res;
// else
throw {
status: res.status,
error_message: res.statusText,
};
}
function api(endpoint, options) {
const url = `https://api.linkedin.com/${this.version}/${endpoint}`;
options = {
method: options && options.method || 'GET',
body: options && options.body && JSON.stringify(options.body),
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.access_token}`,
},
};
return fetch(url, options)
.then(checkStatus)
.then(res => res.json());
}
const { assert } = require('chai');
const LinkedIn = require('../index.js');
const debug = require('debug')('linkedin-api-wrapper:test');
const ACCESS_TOKEN=process.env.LINKEDIN_ACCESS_TOKEN;
describe("linkedin", function() {
const linkedin = LinkedIn({
access_token: ACCESS_TOKEN,
});
this.timeout(5000);
it("should return an object on success", () => {
return linkedin.api('me')
.then((body) => assert.equal(typeof body, 'object'));
});
it("should return an object holding the answer", () => {
return linkedin.api('me')
.then((body) => {
debug(body);
assert.property(body, 'id');
assert.property(body, 'localizedFirstName');
assert.property(body, 'localizedLastName');
});
});
it("should allow posting", async () => {
author = await linkedin.api('me').then(body => body.id)
return linkedin.api('ugcPosts', {
method: 'POST',
body: {
"author": `urn:li:person:${author}`,
"lifecycleState": "PUBLISHED",
"specificContent": {
"com.linkedin.ugc.ShareContent": {
"shareCommentary": {
"text": "Check out my website -- https://s-lungelo.netlify.app",
},
"shareMediaCategory": "NONE",
}
},
"visibility": {
"com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
}
},
})
.then((body) => {
debug(body);
assert.property(body, 'id');
assert.match(body.id, /^urn:li:share:/);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment