1.0.3 • Published 5 months ago

simple-linkedin-post v1.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

🚀 Usage

simple-linkedin-post is a Node.js package designed to simplify LinkedIn posting, focusing on image and video posts.

🛠️ Setup

Package Initialization

import LinkedInApi from 'simple-linkedin-post';
import express from 'express';
import multer from 'multer';

Initialize Express App

const app = express();
const port = 8081; // Change to any available port number

Setup Multer for File Uploads

const storage = multer.memoryStorage();
const upload = multer({ storage: storage });

Configure LinkedIn API Credentials

const clientID = "xxxx" 
const clientSecret = "xxxx"
const redirectURL = "xxxx"
const scope  = "xxxxx"

Initialize LinkedIn API

const linkedIn = new LinkedInApi(clientID, clientSecret, redirectURL, scope);
linkedIn.auth(app);

📝 Examples

Image Post Creation

This example demonstrates how to create an image post on LinkedIn.

app.post('/createImgPost', upload.single('file'), async (req, res) => {

    const accessToken = "xxx"

    if (!accessToken) {
        return res.status(400).json({ message: 'Access token is missing.' });
    }

    if (req.file) {
        try {
            const fileBuffer = req.file.buffer;
            const response = await linkedIn.createImagePost(
                "T0qsRQ6IIP", // User ID
                {
                    imgTitle: "Sample Image",
                    imgDescription: "This is an image description",
                    displayContent: "Feeling inspired after meeting so many talented individuals at this year's conference. #talentconnect"
                },
                accessToken,
                fileBuffer
            );
            return res.json(response);
        } catch (error) {
            console.error('Error creating image post:', error);
            return res.status(500).json({
                message: 'Failed to create image post.',
                error: error.message,
            });
        }
    } else {
        return res.status(400).json({ message: 'No file uploaded.' });
    }
});

Video Post Creation

This example demonstrates how to create an video post on LinkedIn.

app.post('/createVideoPost', upload.single('file'), async (req, res) => {

    const accessToken = "xxx"

    if (!accessToken) {
        return res.status(400).json({ message: 'Access token is missing.' });
    }

    if (req.file) {
        try {
            const fileBuffer = req.file.buffer;
            const response = await linkedIn.createVideoPost(
                "T0qsRQ6IIP", // User ID
                {
                    imgTitle: "Sample Video",
                    imgDescription: "This is a video description",
                    displayContent: "Feeling inspired after meeting so many talented individuals at this year's conference. #talentconnect"
                },
                accessToken,
                fileBuffer
            );
            return res.json(response);
        } catch (error) {
            console.error('Error creating video post:', error);
            return res.status(500).json({
                message: 'Failed to create video post.',
                error: error.message,
            });
        }
    } else {
        return res.status(400).json({ message: 'No file uploaded.' });
    }
});

🔑 Get Access Token

  1. Start the application and navigate to the browser.

    Visit the following URL to be redirected to the LinkedIn login page:

    http://localhost:8081/linkedin/auth
  2. Login to LinkedIn: After logging in successfully, you will be redirected to the callback URL to obtain the access token.

    The callback URL is:

    http://localhost:8081/linkedin/callback
1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago