Using OAuth to Post A Tweet on Twitter
Purpose
The simplest Javascript step-by-step guide with full working code (< 15 lines) to use OAuth to post a tweet on behalf of a Twitter user through OAuth.io
Full Code to Use OAuth to Post Tweet on Twitter
Here is the final code - a Javascript snippet that you can tweak, and play around with instantly without any coding, configuration, etc.
Details of each part of the code:
HTML
<a id="twitter-button" class="btn btn-block btn-social btn-twitter">
<i class="fa fa-twitter"></i> Post a Tweet on Twitter
</a>
CSS
None
Javascript
$('#twitter-button').on('click', function() {
// Initialize with your OAuth.io app public key
OAuth.initialize({Your OAuth.io App Key});
// Use popup for OAuth
OAuth.popup('twitter').then(twitter => {
// OAuth.io Twitter object
console.log('twitter:', twitter);
// Use OAuth.io Twitter object to post a tweet
twitter.post({
url: "/1.1/statuses/update.json?status=Some%20random%20Tweet"
}).then(tweet => {
console.log('Created Tweet:', tweet);
});
});
})
External Requirements
This Javascript snippet relies on a few libraries:
- 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., Facebook, 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 post a Tweet on Twitter on a user's behalf using OAuth2.
- Create a Twitter account, if you do not have one
- Create an app on Twitter using this simple step-by-step guide
- Copy your Twitter app client id and client secret
- Signup for OAuth.io account
- Link your Twitter app with OAuth.io by pasting your Twitter app client id and client secret into OAuth.io
- Copy the OAuth javascript snippet (https://jsfiddle.net/L6t27c94) on to your web page
References
Use OAuth to Post Tweet on Twitter - https://tome.oauth.io/providers/twitter/post-a-tweet
Written by Khor
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#