Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ const fs = require('fs');
const auth_base_url = process.env.AUTH_BASE_URL;
const client_id = process.env.CLIENT_ID;
const client_secret = process.env.CLIENT_SECRET;
const token = process.env.ACCESS_TOKEN;
const redirect_uri = process.env.REDIRECT_URI;
const response_type = 'code';
const state = Math.random(); // WARNING: using weak random value for testing ONLY
const scope = 'r_liteprofile r_emailaddress w_member_social';
const scope = 'profile w_member_social openid email';

const app = http.createServer(function (req, res) {

let req_pathname = url.parse(req.url, true).pathname;
let req_query = url.parse(req.url, true).query;

let redirect_uri_pathname = (new URL(redirect_uri)).pathname;

console.log('Request URL: ' + redirect_uri_pathname);
// get authorization code - the server redirects to the linkedin sign-in page
console.log(req_pathname)
if(req_pathname == '/') {
let auth_url = auth_base_url + '?response_type=' + response_type + '&client_id=' + client_id + '&redirect_uri=' + encodeURIComponent(redirect_uri) + '&state=' + state + '&scope=' + encodeURIComponent(scope);
let auth_url = auth_base_url + '?response_type=' + response_type + '&client_id=' + client_id + '&redirect_uri=' + encodeURIComponent(redirect_uri) + '&code=' + token + '&state=' + state + '&scope=' + encodeURIComponent(scope);
res.writeHead(302, {'Location': auth_url})
res.end();
}
Expand All @@ -32,7 +34,7 @@ const app = http.createServer(function (req, res) {

let req_code = req_query.code;
let req_state = req_query.state;

console.log("CODE:>>", req_query.code)
// WARNING: test req_state == state to prevent CSRF attacks

let path_query =
Expand All @@ -46,10 +48,11 @@ const app = http.createServer(function (req, res) {
let hostname = 'www.linkedin.com';
let path = '/oauth/v2/accessToken?' + path_query;
let headers = {
"Content-Type": "x-www-form-urlencoded"
"Content-Type": "application/x-www-form-urlencoded"
};
let body = '';
_request(method, hostname, path, headers, body).then(r => {
console.log('Response: ' + r.status);
if(r.status == 200){
let access_token = JSON.parse(r.body).access_token;
let expires_in = Date.now() + (JSON.parse(r.body).expires_in * 1000); // token expiry in epoc format
Expand Down Expand Up @@ -103,6 +106,7 @@ function _request(method, hostname, path, headers, body){
resBody += data.toString('utf8');
});
res.on('end', () => {
console.log(resBody)
resolve({
"status": res.statusCode,
"headers": res.headers,
Expand Down
52 changes: 34 additions & 18 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ getLinkedinId(accessToken).then(ownerId => {
function getLinkedinId(accessToken) {
return new Promise((res, rej) => {
let hostname = 'api.linkedin.com';
let path = '/v2/me';
// let path = '/v2/me';
let path = '/v2/userinfo';
let method = 'GET';
let headers = {
'Authorization': 'Bearer ' + accessToken,
Expand All @@ -37,7 +38,7 @@ function getLinkedinId(accessToken) {
};
let body = ''
_request(method, hostname, path, headers, body).then(r => {
res(JSON.parse(r.body).id)
res(JSON.parse(r.body).sub)
}).catch(e => rej(e))
})
}
Expand All @@ -46,25 +47,40 @@ function getLinkedinId(accessToken) {
function postShare(accessToken, ownerId, title, text, shareUrl, shareThumbnailUrl) {
return new Promise((res, rej) => {
let hostname = 'api.linkedin.com';
let path = '/v2/shares';
let path = '/v2/ugcPosts';
let method = 'POST';
// let body = {
// "owner": "urn:li:person:" + ownerId,
// "subject": title,
// "text": {
// "text": text // max 1300 characters
// },
// "content": {
// "contentEntities": [{
// "entityLocation": shareUrl,
// "thumbnails": [{
// "resolvedUrl": shareThumbnailUrl
// }]
// }],
// "title": title
// },
// "distribution": {
// "linkedInDistributionTarget": {}
// }
// }
let body = {
"owner": "urn:li:person:" + ownerId,
"subject": title,
"text": {
"text": text // max 1300 characters
"author": "urn:li:person:" + ownerId,
"lifecycleState": "PUBLISHED",
"specificContent": {
"com.linkedin.ugc.ShareContent": {
"shareCommentary": {
"text": text
},
"shareMediaCategory": "NONE"
}
},
"content": {
"contentEntities": [{
"entityLocation": shareUrl,
"thumbnails": [{
"resolvedUrl": shareThumbnailUrl
}]
}],
"title": title
},
"distribution": {
"linkedInDistributionTarget": {}
"visibility": {
"com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
}
}
let headers = {
Expand Down
19 changes: 15 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.