Using OAuth to Share Post Or Link on LinkedIn
Goal
Provide the easiest way (less than 15 lines of code) to use OAuth to share a post or link on a LinkedIn account on behalf of a LinkedIn user using Javascript.
Code Example
You can test this snippet by trying to login with your LinkedIn account, or tweak it without requiring any configuration, coding, etc.
https://jsfiddle.net/ntu1qyg7/
Code Explanation
HTML
<a id="linkedin-button" class="btn btn-block btn-social btn-vimeo">
<i class="fa"></i> Share a Post with LinkedIn
</a>
CSS
None
Javascript
$('#linkedin-button').on('click', function() {
// Initialize with your OAuth.io app public key
OAuth.initialize('YOUR_OAUTH_IO_KEY');
OAuth.popup('linkedin2').then(linkedin => {
linkedin.post({
url: "/v1/people/~/shares?format=json",
data: JSON.stringify({
"comment": "Hello world!",
"visibility": {
"code": "anyone"
}
}),
headers: {
"x-li-format": "json",
"Content-Type": "application/json"
}
}).then(data => {
console.log("success:", data);
}).fail(err => { console.log("err:",err) });
})
})
External Requirements
This Javascript code has external requirements:
- jQuery - HTML element manipulation
- bootstrap, font-awesome - Look-and-feel of social buttons and icons
- oauth-io javascript library - Simplifies OAuth usage by hiding the nuances in the OAuth implementation of various OAuth providers, e.g., LinkedIn, Twitter, etc. behind the ratified standard OAuth2 interface
Step-by-step Guide
Here is a summary of the steps to create this social button to share a post or link on LInkedIn on a user's behalf using OAuth2.
- Create a LinkedIn account, if you do not have one
- Create an app on LinkedIn using this simple step-by-step guide
- Copy your LinkedIn app client id and client secret
- Signup for OAuth.io account
- Link your LinkedIn app with OAuth.io by pasting your LinkedIn app client id and client secret into OAuth.io
- Copy the OAuth javascript snippet (https://jsfiddle.net/ntu1qyg7/) on to your web page
References
Use OAuth to share a post or link on LinkedIn: https://tome.oauth.io/providers/linkedin/share-a-link
Written by Khor
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Oauth
Authors
Related Tags
#oauth
#oauth2
#javascript
#linkedin
#share link
#share post
#bootstrap
#social login
#button
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#