Community Articles Enabling Twitter Search in Cosmic JS

Enabling Twitter Search in Cosmic JS

TL;DR
Install the Twitter Search Extension
View Source Code


Intro

In this article, we will walk through how to build a Cosmic JS Extension that can search and embed a Twitter timeline in our dashboard, and save selected Tweets from the timeline to our Bucket.  We will utilize a couple of Twitter APIs to build our customized Twitter search API for our extension. This Cosmic JS Extension greatly simplifies the workflow for developers who need to embed Tweets into their websites based on dynamic Twitter search queries.


Get Twitter consumer key and consumer secret 

To be able to use the Twitter APIs, we need to have a copy of Twitter consumer key and consumer secret. To retrieve these, log into your Twitter account, then go to apps.twitter.com and apply for a developer account.

After getting approval from Twitter, create a new application in your developer account and fill out this form:

Next, Twitter will generate your consumer key and secret which are displayed under 'Details' tab. Keep these since we will use them to implement our own search API. 


Implement Twitter Authentication 

Anyone who uses our Cosmic JS Twitter Extension needs to sign in Twitter to be authenticated. Twitter uses oAuth 1.0 for authentication and authorization. Our extension redirects our users to a Twitter sign in and consent popup page. Upon our users' successful login to Twitter and consenting Cosmic JS to send Twitter APIs on their behalf, users will be returned to the dashboard and able to perform Twitter search.     


Develop our customized Twitter search API

As we mentioned above, Twitter does not provide an API for searching and embedding search results. We will develop our own customized API by using the combination of two APIs provided by Twitter. The two Twitter APIs that will become our building blocks are:

1, Twitter search API that gets a list of relevant Tweets matching a search query, in JSON.

Example from Twitter API documentation:  

2, API for getting the HTML of a Tweet given its author and Tweet ID, ready to be embedded on a website. 

Example from Twitter documentation: 

 

The basic idea is to use the Twitter search API to get a list of Tweets in JSON. The results return from the Twitter search API does not contain the HTML of the Tweets, so we cannot directly embed the search results into Cosmic JS. This is where the second Twitter API comes into play. For each Tweet returned from the first API, we extract its author ID and Tweet ID, and use these information to invoke the second API to get its HTML. Here is the code: 


The rest is straightforward. We can now invoke our own search API from the browser and embed the results that are just a list of HTML blocks in Cosmic JS. A Tweet Object only has one Metafield which is “html-textarea” type. Invoke the relevant Cosmic JS APIs to add or delete Tweet Object Type and Tweet Objects in Cosmic JS dashboard. If you have any doubts of how to use the Cosmic JS APIs, please check out the Cosmic JS API documentation. 

Conclusion
I hope you found this tutorial helpful in demonstrating how Cosmic JS Extensions allow you to extend the power of your Cosmic JS Bucket and communicate with third-party APIs like Twitter. Join the conversation on the Cosmic JS Slack channel and reach out to Cosmic JS on Twitter.

Was this article helpful?

Thank you! Your input helps us improve our articles and resources. If you have any other questions or comments, please contact support.

You may also like


Build a fully functioning messenger that can send SMS messages to application users. We will discuss Server side rendering of a React front, and leveraging a simple api in order to make protected requests to Cosmic JS cloud services.
Our digital world is being built on top of API services. And Webhooks are the operators that enable these API services to communicate with each other.
In this tutorial, we're going to build a digital shrine to the great musical artists of our time and to listening to music by album - the way great artists meant it to be listened to! In the process, you'll learn a little bit about Cosmic JS, React, CSS Grid, Flexbox, Material UI and Spotify's aweso
This article demonstrates how to create a company website using Cosmic JS and Angular in a few steps.
Thanks to everyone in Dallas who came out to the Vuetiful Holiday Meetup at Simple Media, featuring Vuetify, Netlify and Cosmic JS. 🎉
Cosmic JS provides tools and resources to create your own unique content publishing workflow that satisfies your team's needs. In this tutorial I'll demonstrate setting up a simple workflow for an Editor and Contributor team that needs to manage content in a draft, review and publish process.

Ready to Get Started?

No payment info required.
Start Now   Contact Sales