linkedin-post-share
Advanced tools
Comparing version 1.1.14 to 1.1.15
@@ -11,3 +11,4 @@ /// <reference types="node" /> | ||
private uploadImage; | ||
private removeLinkedinReservedCharacters; | ||
createPostWithImage(post: string, image: Buffer, imageAlt?: string): Promise<boolean | undefined>; | ||
} |
@@ -1,1 +0,1 @@ | ||
var s=(p,r,e)=>new Promise((t,n)=>{var i=o=>{try{c(e.next(o))}catch(a){n(a)}},h=o=>{try{c(e.throw(o))}catch(a){n(a)}},c=o=>o.done?t(o.value):Promise.resolve(o.value).then(i,h);c((e=e.apply(p,r)).next())});import l,{AxiosError as u}from"axios";var d=class{constructor(r){this.accessToken=r;this.LINKEDIN_BASE_URL="https://api.linkedin.com";this.LINKEDIN_VERSION="202303"}getProfileData(){return s(this,null,function*(){var r;try{return(yield l(`${this.LINKEDIN_BASE_URL}/v2/me`,{method:"GET",headers:{Authorization:`Bearer ${this.accessToken}`}})).data}catch(e){return e instanceof u?(console.error("Cannot get profile data. Error: ",(r=e.response)==null?void 0:r.data),null):(console.error("Something went wrong. Error: ",e),null)}})}getPersonURN(){return s(this,null,function*(){let r=yield this.getProfileData();return!r||!r.id?null:`urn:li:person:${r.id}`})}createImageUploadRequest(r){return s(this,null,function*(){var e;try{return(yield l(`${this.LINKEDIN_BASE_URL}/rest/images?action=initializeUpload`,{method:"POST",headers:{"LinkedIn-Version":this.LINKEDIN_VERSION,Authorization:`Bearer ${this.accessToken}`,"X-Restli-Protocol-Version":"2.0.0"},data:{initializeUploadRequest:{owner:r}}})).data}catch(t){if(t instanceof u){console.error("Cannot create image upload request. Error: ",(e=t.response)==null?void 0:e.data);return}console.error("Something went wrong. Error: ",t)}})}uploadImage(r,e){return s(this,null,function*(){try{let t=yield l(e,{method:"PUT",headers:{"Content-Type":"application/octet-stream",Authorization:`Bearer ${this.accessToken}`},data:r});return t.status!==201?(console.error("Image not created. Status code: ",t.status),!1):!0}catch(t){if(t instanceof u){console.error("Cannot upload image. Error: ",t.cause);return}console.error("Something went wrong. Error: ",t)}})}createPostWithImage(r,e,t){return s(this,null,function*(){var o;let n=yield this.getPersonURN();if(!n){console.error("Cannot get person URN");return}let i=yield this.createImageUploadRequest(n);if(!i){console.error("Cannot create image upload request");return}if(!(yield this.uploadImage(e,i.value.uploadUrl))){console.error("Cannot upload the image");return}let c=i.value.image;try{let a={author:n,commentary:r,visibility:"PUBLIC",distribution:{feedDistribution:"MAIN_FEED",targetEntities:[],thirdPartyDistributionChannels:[]},content:{media:{title:t!=null?t:"Cover image of the post",id:c}},lifecycleState:"PUBLISHED",isReshareDisabledByAuthor:!1};console.log(a);let g=yield l(`${this.LINKEDIN_BASE_URL}/rest/posts`,{method:"POST",headers:{"LinkedIn-Version":this.LINKEDIN_VERSION,Authorization:`Bearer ${this.accessToken}`,"Content-Type":"application/json","X-Restli-Protocol-Version":"2.0.0"},data:a});return g.status!==201?(console.error("Image not created. Status code: ",g.status),!1):!0}catch(a){if(a instanceof u){console.error("Cannot create post. Error: ",(o=a.response)==null?void 0:o.data);return}console.error("Something went wrong. Error: ",a)}})}};export{d as default}; | ||
var s=(h,e,t)=>new Promise((r,o)=>{var i=a=>{try{c(t.next(a))}catch(n){o(n)}},m=a=>{try{c(t.throw(a))}catch(n){o(n)}},c=a=>a.done?r(a.value):Promise.resolve(a.value).then(i,m);c((t=t.apply(h,e)).next())});import u,{AxiosError as d}from"axios";var p=class{constructor(e){this.accessToken=e;this.LINKEDIN_BASE_URL="https://api.linkedin.com";this.LINKEDIN_VERSION="202303"}getProfileData(){return s(this,null,function*(){var e;try{return(yield u(`${this.LINKEDIN_BASE_URL}/v2/me`,{method:"GET",headers:{Authorization:`Bearer ${this.accessToken}`}})).data}catch(t){return t instanceof d?(console.error("Cannot get profile data. Error: ",(e=t.response)==null?void 0:e.data),null):(console.error("Something went wrong. Error: ",t),null)}})}getPersonURN(){return s(this,null,function*(){let e=yield this.getProfileData();return!e||!e.id?null:`urn:li:person:${e.id}`})}createImageUploadRequest(e){return s(this,null,function*(){var t;try{return(yield u(`${this.LINKEDIN_BASE_URL}/rest/images?action=initializeUpload`,{method:"POST",headers:{"LinkedIn-Version":this.LINKEDIN_VERSION,Authorization:`Bearer ${this.accessToken}`,"X-Restli-Protocol-Version":"2.0.0"},data:{initializeUploadRequest:{owner:e}}})).data}catch(r){if(r instanceof d){console.error("Cannot create image upload request. Error: ",(t=r.response)==null?void 0:t.data);return}console.error("Something went wrong. Error: ",r)}})}uploadImage(e,t){return s(this,null,function*(){try{let r=yield u(t,{method:"PUT",headers:{"Content-Type":"application/octet-stream",Authorization:`Bearer ${this.accessToken}`},data:e});return r.status!==201?(console.error("Image not created. Status code: ",r.status),!1):!0}catch(r){if(r instanceof d){console.error("Cannot upload image. Error: ",r.cause);return}console.error("Something went wrong. Error: ",r)}})}removeLinkedinReservedCharacters(e){return e.replace(/[|{}@\[\]()<>\\*_~+]/gm,"")}createPostWithImage(e,t,r){return s(this,null,function*(){var n;let o=yield this.getPersonURN();if(!o){console.error("Cannot get person URN");return}let i=yield this.createImageUploadRequest(o);if(!i){console.error("Cannot create image upload request");return}if(!(yield this.uploadImage(t,i.value.uploadUrl))){console.error("Cannot upload the image");return}let c=i.value.image,a=this.removeLinkedinReservedCharacters(e);try{let l={author:o,commentary:a,visibility:"PUBLIC",distribution:{feedDistribution:"MAIN_FEED",targetEntities:[],thirdPartyDistributionChannels:[]},content:{media:{title:r!=null?r:"Cover image of the post",id:c}},lifecycleState:"PUBLISHED",isReshareDisabledByAuthor:!1},g=yield u(`${this.LINKEDIN_BASE_URL}/rest/posts`,{method:"POST",headers:{"LinkedIn-Version":this.LINKEDIN_VERSION,Authorization:`Bearer ${this.accessToken}`,"Content-Type":"application/json","X-Restli-Protocol-Version":"2.0.0"},data:l});return g.status!==201?(console.error("Image not created. Status code: ",g.status),!1):!0}catch(l){if(l instanceof d){console.error("Cannot create post. Error: ",(n=l.response)==null?void 0:n.data);return}console.error("Something went wrong. Error: ",l)}})}};export{p as default}; |
{ | ||
"name": "linkedin-post-share", | ||
"version": "1.1.14", | ||
"version": "1.1.15", | ||
"description": "A Linkedin Wrapper for Post/Share API. This will help you to post content on linkedin.", | ||
@@ -5,0 +5,0 @@ "repository": { |
# LinkedIn Post Share | ||
A Node.js package for sharing a post on LinkedIn with an image. | ||
@@ -7,2 +8,3 @@ | ||
## Installation | ||
To install the package, use NPM: | ||
@@ -12,5 +14,6 @@ | ||
npm install linkedin-post-share | ||
``` | ||
``` | ||
## Usage | ||
To use the package, you will need to obtain an access token for the LinkedIn API. You can do this by following the instructions in the [LinkedIn API documentation](https://learn.microsoft.com/en-us/linkedin/shared/authentication/authorization-code-flow). | ||
@@ -39,3 +42,9 @@ | ||
``` | ||
### 🚨 NOTE | ||
__This package will automatically remove any reserved characters that are mentioned in the [Linkedin Docs](https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/little-text-format?view=li-lms-2023-03#text) like `[|{}@\[\]()<>\\*_~+]` from the post string.__ | ||
### API | ||
#### LinkedinPostShare | ||
@@ -48,8 +57,8 @@ | ||
- post - The text of the post. | ||
- image - A Buffer. | ||
- imageAlt - Optional alternative text for the image. If not provided, a default value will be used. | ||
- post - The text of the post. | ||
- image - A Buffer. | ||
- imageAlt - Optional alternative text for the image. If not provided, a default value will be used. | ||
### Contributing | ||
Contributions to this package are welcome! If you find a bug or would like to request a new feature, please open an issue on the GitHub repository. If you would like to contribute code, please open a pull request. | ||
Contributions to this package are welcome! If you find a bug or would like to request a new feature, please open an issue on the GitHub repository. If you would like to contribute code, please open a pull request. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10881
40
61