In my last blog post I did an introduction to “how use the starter kit for Node-RED on IBM Cloud” and in this new blog post I want to highlight the Node-RED Twitter-follower-flow I developed as an introduction to different topics I frequently face at hackathons. Here is the link to the “Twitter-follower-flow” GitHub project. Node-RED is very good for prototyping, that is the reason why it is often used in hackathons. If you are new to Node-RED and you start to develop a Node-RED flow, you normally have following challenges:
- How to …
- … define own REST endpoints to encapsulate an external API?
- … automate the authentication to that external API?
- … extract data from the external API?
- … customize data and CRUD with databases?
The Node-RED flow of that project has the objective to provide an (little advanced) introduction to the first three topics above. The CRUD (Create, Read, Update, Delete) topic is not covered here. Visit that CRUD example for more information.
- The “Twitter-follower-flow” example …
- … uses open technologies (Node-RED is a Project of the OpenJS foundation )
- … creates no additional costs
- Uses a free runtime on IBM Cloud
- Uses the free Twitter developer API
- … has following technical level:
- Beginner to intermediate
- … takes 30 – 45 min to setup the example from scratch
- Register on IBM Cloud
- Create a Node-RED instance on IBM Cloud
- Register at Twitter for a developer API Account
- Copy the existing Node-RED flow
- Configure the flow
- Run the flow
The YouTube video below gives you a 13 min more detailed introduction to the Twitter-follower-flow.
1. The UseCase
The UseCase “Extract the twitter follower list” addresses three topics listed above in the Node-RED Twitter-follower-flow.
- Define own REST endpoints to encapsulate Twitter developer API calls.
- Automate the authentication the two step authentication of the Twitter developer API.
- Extract data from the Twitter follower list and build an own list.
2. Topics related to the Node-RED usage
The Twitter-follower-flow is an concrete example of following topics …
- … the implementation of Node-RED REST endpoints.
- Create a very basic authentication to protect each Node-RED endpoint
- Realize the two step authentication (also known as two-factor authentication) to access the Twitter developer API:
- Basic authentication with a key and secret to request a bearer token from Twitter
- Use the bearer token for authentication to use the Twitter API
- Get the follower list from Twitter using the bearer token
- … the usage of the implemented REST endpoints to automate following sequence to get the Twitter follower.
- Get the authorization bearer token from Twitter to access the Twitter API
- Get all followers from Twitter using a bearer token ( the challenge is: how implement the paging thought the followers list from Twitter )
- Extract the names of the follower from the follower list
- … the work with following Nodes in the Node-RED Twitter-follower-flow.
- Inject (Start a flow in Node-RED.)
- HTTP in (Creates an HTTP end-point for creating web services.)
- HTTP request (Sends HTTP requests and returns the response.)
- HTTP response (Sends responses back to requests received from an HTTP Input node.)
- Base64 (A function that converts the chosen property (default msg.payload) to and from base64 format.)
- Switch (Define a decision how to route the payload. Route messages based on their property values or sequence position.)
- … the usage of flow variables to exchange values between functions in the Twitter-follower-flow
3. Setup the Twitter-follower-flow example
That section contains six major steps to setup the Twitter-follower-flow example on IBM Cloud.
- Setup Twitter developer application
- Create a Node-RED instance of IBM Cloud
- Install a additional Node to the Node-RED instance
- Import the Node-RED flow
- Configure the Node-RED flow
- Introduction to the Node-RED flow (13 min video)
In the following image you see the full flow. The green arrows showing the invocation of the Node-RED endpoints, from the automated flow to get the Twitter follower list.
Step 1: Setup Twitter developer application
You need a Twitter account and a Twitter developer application. With your Twitter account you can register for the Twitter developer API and create a Twitter application. That application provides the needed credentials to access the Twitter API, which is used in that example.
a. Link to add a Twitter developer application.
Here is a blog post with an example how to setup a Twitter application: How to Register a Twitter App in 8 Easy Steps
Here is a preview, how to get the credentials from your Twitter developer application:
b. Twitter API documentation
Step 2: Create a Node-RED instance on IBM Cloud
We use a Node-RED instance on IBM Cloud with an IBM Lite Account.
- Create an IBM Cloud Lite Account just by register here.
- Follow the steps in my blog post to setup a Node-RED instance on IBM Cloud
Step 3: Install a additional Node to the Node-RED instance
node-red-node-base64 Node to the Node-RED instance. The gif shows the installation.
Step 4: Import the Node-RED flow
Import the flow from the flow.json file in
flows folder of that project. The gif below shows how to import the flow in the Node-RED.
Step 5: Configure the Node-RED flow
We need to configure …
- … the Twitter authentication
- … the Twitter username
- … the Node-RED URL
a. Set Twitter API key and secret
Insert the values for the
secret of the Twitter API credentials in the function
set user and secret. The image shows an example, where you get the credentials information.
The mapping for the Node-RED flow:
user= Twitter API key
secret= Twitter API secret key
nodereduser= Your own definition to secure the Node-RED REST Endpoints
noderedpassword= Your own definition to secure the Node-RED REST Endpoints
Here is the source code for the
set user and secret function.
var user = "USER"; var secret = "SECRET" var nodereduser = "admin"; var noderedpassword = "notreallysecure" flow.set("nodereduser", nodereduser); flow.set("noderedpassword", noderedpassword); msg.payload = user + ":" + secret; return msg;
b. Set Twitter username
Define “username display name” you want get the follower list from, by inserting in function
// Set basic auth flow.set("auth", msg.payload); msg.payload = flow.get("auth"); // Init parameters: // For more information visit get-followers-list: // https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-followers-list flow.set("twitterCursor", "-1"); flow.set("twitterResultCount", "50"); flow.set("twitterSN", "YOUR_TWITTER_DISPLAY_NAME"); msg.nodereduser = flow.get("nodereduser"); msg.noderedpassword = flow.get("noderedpassword"); return msg;
c. Set Node-RED URL
Configure the HTTP request nodes. Replace the
https://node-red-my-hackathon.mybluemix.net URL with your URL in each HTTP request node:
The image shows how you enter the URL in the HTTP requests node
That image shows HTTP requests nodes to be change.
Step 6: Introduction to the Node-RED flow
The gifs shows a execution of the Twitter-follower-flow:
The YouTube video gives you a 13 min introduction to the Twitter-follower-flow.
I hope this was useful for you and let’s see what’s next?
PS: You can try out Cloud Foundry Apps or Kubernetes on IBM Cloud. By the way, you can use the IBM Cloud for free, if you simply create an IBM Lite account. Here you only need an e-mail address.
#IBMDeveloper, #IBMCloud, #Node-RED, #REST, #Endpoints, #HTTP, #TwitterAPI, #Node-RED-flow, #hackathon
Leave a Reply