Accessing a Twitter timeline via the Twitter REST API

This flow demonstrates the process of accessing a user timeline using the Twitter REST API. The README contained within the flow walks you through the process of generating a bearer token for accessing the API. Once the token is generated and configured within the flow, you can use the flow to issue Twitter API requests.

Usage: GET /getUserTweets?screen_name=&count=10

Example:

GET /getUserTweets?screen_name=bsilverm_IoT&count=10

Parameters: ?screen_name - The screen name of the Twitter user ?count (optional) - The number of tweets to retrieve from the user

Example generated request: (for API usage see Twitter doc @ https://dev.twitter.com/rest/reference/get/statuses/user_timeline):

https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=bsilverm_IoT&count=10

The authentication method used is "Application-Only". For more details, see:

https://dev.twitter.com/oauth/application-only

[{"id":"62cfa4bd.1121cc","type":"http in","name":"/getUserTweets?screen_name=<xyz>&count=<n>","url":"/getUserTweets","method":"get","swaggerDoc":"","x":255.7777862548828,"y":180.33335399627686,"z":"9e3bbc64.e538f8","wires":[["22c1d43.1f2682c"]]},{"id":"b3d263f4.219e","type":"comment","name":"README","info":"Pre-req: Complete the instructions found in \"Request a Bearer token\" \n\nUsage: \nGET /getUserTweets?screen_name=<twitter screen name>\n\nExample:\n\nGET /getUserTweets?screen_name=bsilverm_IoT\n\nParameters:\n?screen_name - The screen name of the Twitter user\n?count (optional) - The number of tweets to retrieve from the user\n\nExample generated request: (for API usage see Twitter doc @ https://dev.twitter.com/rest/reference/get/statuses/user_timeline):\n\nhttps://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=bsilverm_IoT&count=10","x":550.8888854980469,"y":118.88891410827637,"z":"9e3bbc64.e538f8","wires":[]},{"id":"abb684bb.4f0d68","type":"http request","name":"Issue Twitter API REST Request (GET)","method":"GET","ret":"txt","url":"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={{{twitterSN}}}&count={{{count}}}","x":831.9998054504395,"y":180.3333225250244,"z":"9e3bbc64.e538f8","wires":[["a82465c9.3197d"]]},{"id":"22c1d43.1f2682c","type":"function","name":"Twitter AUTH headers","func":" msg.headers = {\n     'Authorization' : '<YOUR GENERATED BEARER TOKEN HERE>',\n     'Content-Type' : 'application/x-www-form-urlencoded',\n  }\nmsg.twitterSN =  msg.req.query.screen_name;\nmsg.count = msg.req.query.count;\nreturn msg;","outputs":1,"noerr":0,"x":558.1388549804688,"y":179.94443321228027,"z":"9e3bbc64.e538f8","wires":[["abb684bb.4f0d68"]]},{"id":"b52fb04a.12472","type":"http in","name":"/getToken","url":"/getToken","method":"get","swaggerDoc":"","x":246.88890075683594,"y":322.4222354888916,"z":"9e3bbc64.e538f8","wires":[["e39dd0d.fddd43"]]},{"id":"c8daac5a.5385d","type":"http request","name":"Request Bearer Token","method":"POST","ret":"txt","url":"https://api.twitter.com/oauth2/token","x":567.138916015625,"y":322.6723117828369,"z":"9e3bbc64.e538f8","wires":[["a5d9b00d.48f6d"]]},{"id":"e39dd0d.fddd43","type":"function","name":"headers","func":" msg.headers = {\n     'Authorization' : 'Basic YmkzYWpyMFNaelg0Vmh1RU5SbzFpcWpnNTpqcTJjbnBZNUlFVDVCck5JUGFTVlFmVHBDVDJKM0EwZlRzR2JpRVBYVDNCVGJKdTVFMg==',\n     'Content-Type' : 'application/x-www-form-urlencoded'\n  };\n  \nmsg.payload = 'grant_type=client_credentials';\nreturn msg;","outputs":1,"noerr":0,"x":383.88890075683594,"y":322.42225074768066,"z":"9e3bbc64.e538f8","wires":[["c8daac5a.5385d"]]},{"id":"a5d9b00d.48f6d","type":"http response","name":"Token Response","x":784.138916015625,"y":322.6723117828369,"z":"9e3bbc64.e538f8","wires":[]},{"id":"83fdb401.198ef","type":"comment","name":"Request a Bearer Token from Twitter (readme)","info":"Steps:\n\n1)  Go to apps.twitter.com and create an application.\n2)  Copy your consumer key and secret.\n3)  Build a string in this format: \"consumer key:secret\" (ie - \"12345:6789\")\n4)  Encode the string in base64 using any base64 encoding utility (some available on the web)\n5)  Double-click the \"headers\" node and paste your encoded string on line 2\nafter the word \"Basic \".  For example:\n\n'Authorization' : 'Basic cmFuZG9tdGV4dHJhbmRvbXRleHRyYW5kb210ZXh0OnJhbmRvbXRleHRyYW5kb210ZXh0cmFuZG9tdGV4dA==',\n\n6)  Deploy the flow\n7)  Execute the request by entering the following URL in your browser:\n\nhttp://<your node red URL>/getToken\n\n8)  If done correctly, you will be granted a bearer token\n9)  Copy the token in to line 2 of the \"Twitter AUTH headers\" node\n\nThis only needs to be done once, unless for some reason you need to invalidate\nthe token (ie - it is compromised and needs to be re-generated).\n\nFor more information on Application-only Authentication, see:\n\nhttps://dev.twitter.com/oauth/application-only","x":578.1889038085938,"y":262.2222385406494,"z":"9e3bbc64.e538f8","wires":[]},{"id":"a82465c9.3197d","type":"http response","name":"","x":1068.0000486373901,"y":180.22223663330078,"z":"9e3bbc64.e538f8","wires":[]}]
bsilverm

Flow Info

created 2 years, 3 months ago

Node Types

Core
  • comment (x2)
  • function (x2)
  • http in (x2)
  • http request (x2)
  • http response (x2)

Tags

  • Twitter
  • HTTP
  • REST
  • Timeline
  • OAuth
  • token
  • API
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option