Want to schedule a Twitter post for later? Or re-post a message at the same time every day? To get the most out of Twitter, you’ll want to extend it via the Developer API .
Choose a Name, Description, Website and Callback URL. Choose anything you like - even the Callback URL doesn’t matter at this point. Accept the Developer Agreement and click “Create your Twitter application”.
On your application management page, navigate to the “Keys and Access Tokens” tab. Make a note of the “Consumer Key” and “Consumer Secret”. You’re going to need these.
Also note that the “Access Level” is Read-only by default. If you want you application to post Tweets, now is the time to change it to “Read and write”.
Next, scroll to the bottom of the page and click “Create my access token”. This will generate an Access Token and Access Token Secret that let your new App make Twitter API calls on your account’s behalf. Make a note of these too.
Before we move on, let’s add these keys, tokens and secrets to our shell script as constants:
We’ve got all the tokens we need, but before making a request we need to choose a request method and the corresponding parameters.
Now we’ve established the request options, let’s set these up as constants in our shell script too:
Easy right? Well, not quite so easy. Issuing the request above yields the following response:
What went wrong? Well, it turns out that Twitter considers the above request invalid, because there’s no way of knowing:
For our request to work, we need to provide these details to Twitter in the form of an Authorization Header .
Easy. This is the consumer_key we created in Step 2.
According to the documentation, this is “a unique token your application should generate for each unique request” and “any approach which produces a relatively random alphanumeric string should be OK”.
For a one-off, you could just hammer away at the keyboard randomly. But a better approach is to generate the nonce. My approach:
Ugh! This is a nasty one. The signature is created by running all the other request parameters through an annoyingly specific signing algorithm. The parameters include:
Easy peasy. This is always hardcoded to “HMAC-SHA1”.
The number of seconds since UNIX epoch. You can use timestamp to generate this:
This is the access_token we created in Step 2.
Last one (and an easy one). This is always hardcoded to “1.0”.
Finally! Now we’ve got all the parameters together, we can switch them together to create the Authorization Header:
All done! Now you’re ready to make authorized requests against the Twitter API.
[
{
"created_at" : "Tue Feb 24 03:35:14 +0000 2015" ,
"id" : 570064382656253952 ,
"id_str" : "570064382656253952" ,
"text" : "Oh, and current TVs don't support all the #4KBD features like HDR and expanded color gamut. If you want the best quality you should hold off" ,
"source" : " \u 003ca href= \" http: \/\/ twitter.com \/ download \/ iphone \" rel= \" nofollow \"\u 003eTwitter for iPhone \u 003c \/ a \u 003e" ,
"truncated" : false ,
"in_reply_to_status_id" : null ,
"in_reply_to_status_id_str" : null ,
"in_reply_to_user_id" : null ,
"in_reply_to_user_id_str" : null ,
"in_reply_to_screen_name" : null ,
"user" :{
"id" : 165913533 ,
"id_str" : "165913533" ,
"name" : "James Mackenzie" ,
"screen_name" : "jamesfmackenzie" ,
"location" : "London" ,
"profile_location" : null ,
"description" : "Science, Computers, Programming. All good." ,
"url" : "http: \/\/ t.co \/ GUwCHWp6xx" ,
"entities" :{
"url" :{
"urls" :[
{
"url" : "http: \/\/ t.co \/ GUwCHWp6xx" ,
"expanded_url" : "http: \/\/ www.jamesfmackenzie.com" ,
"display_url" : "jamesfmackenzie.com" ,
"indices" :[
0 ,
22
]
}
]
},
"description" :{
"urls" :[
]
}
},
"protected" : false ,
"followers_count" : 733 ,
"friends_count" : 1375 ,
"listed_count" : 31 ,
"created_at" : "Mon Jul 12 21:36:16 +0000 2010" ,
"favourites_count" : 148 ,
"utc_offset" : 0 ,
"time_zone" : "London" ,
"geo_enabled" : true ,
"verified" : false ,
"statuses_count" : 1649 ,
"lang" : "en" ,
"contributors_enabled" : false ,
"is_translator" : false ,
"is_translation_enabled" : false ,
"profile_background_color" : "666666" ,
"profile_background_image_url" : "http: \/\/ pbs.twimg.com \/ profile_background_images \/ 668567178 \/ 9627556962007cb48e3344e03dd8a374.png" ,
"profile_background_image_url_https" : "https: \/\/ pbs.twimg.com \/ profile_background_images \/ 668567178 \/ 9627556962007cb48e3344e03dd8a374.png" ,
"profile_background_tile" : true ,
"profile_image_url" : "http: \/\/ pbs.twimg.com \/ profile_images \/ 531419563422801921 \/ sdtKDzv4_normal.png" ,
"profile_image_url_https" : "https: \/\/ pbs.twimg.com \/ profile_images \/ 531419563422801921 \/ sdtKDzv4_normal.png" ,
"profile_link_color" : "444444" ,
"profile_sidebar_border_color" : "FFFFFF" ,
"profile_sidebar_fill_color" : "DDEEF6" ,
"profile_text_color" : "333333" ,
"profile_use_background_image" : false ,
"default_profile" : false ,
"default_profile_image" : false ,
"following" : false ,
"follow_request_sent" : false ,
"notifications" : false
},
"geo" : null ,
"coordinates" : null ,
"place" : null ,
"contributors" : null ,
"retweet_count" : 0 ,
"favorite_count" : 0 ,
"entities" :{
"hashtags" :[
{
"text" : "4KBD" ,
"indices" :[
42 ,
47
]
}
],
"symbols" :[
],
"user_mentions" :[
],
"urls" :[
]
},
"favorited" : false ,
"retweeted" : false ,
"lang" : "en"
},
{
"created_at" : "Tue Feb 24 03:29:59 +0000 2015" ,
"id" : 570063061479874560 ,
"id_str" : "570063061479874560" ,
"text" : "100GB disk, 2 hour movie = 111Mbit \/ s. So until your broadband \/ wifi combo exceeds that, it's reasonable to expect better quality from #4KBD" ,
"source" : " \u 003ca href= \" http: \/\/ twitter.com \/ download \/ iphone \" rel= \" nofollow \"\u 003eTwitter for iPhone \u 003c \/ a \u 003e" ,
"truncated" : false ,
"in_reply_to_status_id" : null ,
"in_reply_to_status_id_str" : null ,
"in_reply_to_user_id" : null ,
"in_reply_to_user_id_str" : null ,
"in_reply_to_screen_name" : null ,
"user" :{
"id" : 165913533 ,
"id_str" : "165913533" ,
"name" : "James Mackenzie" ,
"screen_name" : "jamesfmackenzie" ,
"location" : "London" ,
"profile_location" : null ,
"description" : "Science, Computers, Programming. All good." ,
"url" : "http: \/\/ t.co \/ GUwCHWp6xx" ,
"entities" :{
"url" :{
"urls" :[
{
"url" : "http: \/\/ t.co \/ GUwCHWp6xx" ,
"expanded_url" : "http: \/\/ www.jamesfmackenzie.com" ,
"display_url" : "jamesfmackenzie.com" ,
"indices" :[
0 ,
22
]
}
]
},
"description" :{
"urls" :[
]
}
},
"protected" : false ,
"followers_count" : 733 ,
"friends_count" : 1375 ,
"listed_count" : 31 ,
"created_at" : "Mon Jul 12 21:36:16 +0000 2010" ,
"favourites_count" : 148 ,
"utc_offset" : 0 ,
"time_zone" : "London" ,
"geo_enabled" : true ,
"verified" : false ,
"statuses_count" : 1649 ,
"lang" : "en" ,
"contributors_enabled" : false ,
"is_translator" : false ,
"is_translation_enabled" : false ,
"profile_background_color" : "666666" ,
"profile_background_image_url" : "http: \/\/ pbs.twimg.com \/ profile_background_images \/ 668567178 \/ 9627556962007cb48e3344e03dd8a374.png" ,
"profile_background_image_url_https" : "https: \/\/ pbs.twimg.com \/ profile_background_images \/ 668567178 \/ 9627556962007cb48e3344e03dd8a374.png" ,
"profile_background_tile" : true ,
"profile_image_url" : "http: \/\/ pbs.twimg.com \/ profile_images \/ 531419563422801921 \/ sdtKDzv4_normal.png" ,
"profile_image_url_https" : "https: \/\/ pbs.twimg.com \/ profile_images \/ 531419563422801921 \/ sdtKDzv4_normal.png" ,
"profile_link_color" : "444444" ,
"profile_sidebar_border_color" : "FFFFFF" ,
"profile_sidebar_fill_color" : "DDEEF6" ,
"profile_text_color" : "333333" ,
"profile_use_background_image" : false ,
"default_profile" : false ,
"default_profile_image" : false ,
"following" : false ,
"follow_request_sent" : false ,
"notifications" : false
},
"geo" : null ,
"coordinates" : null ,
"place" : null ,
"contributors" : null ,
"retweet_count" : 0 ,
"favorite_count" : 0 ,
"entities" :{
"hashtags" :[
{
"text" : "4KBD" ,
"indices" :[
133 ,
138
]
}
],
"symbols" :[
],
"user_mentions" :[
],
"urls" :[
]
},
"favorited" : false ,
"retweeted" : false ,
"lang" : "en"
},
{
"created_at" : "Tue Feb 24 03:27:51 +0000 2015" ,
"id" : 570062525519302656 ,
"id_str" : "570062525519302656" ,
"text" : "Nice primer on #4KBD. \" Ultra HD '4K' Blu-ray: Here's what we know \" http: \/\/ t.co \/ hiQYV94nxg" ,
"source" : " \u 003ca href= \" http: \/\/ bufferapp.com \" rel= \" nofollow \"\u 003eBuffer \u 003c \/ a \u 003e" ,
"truncated" : false ,
"in_reply_to_status_id" : null ,
"in_reply_to_status_id_str" : null ,
"in_reply_to_user_id" : null ,
"in_reply_to_user_id_str" : null ,
"in_reply_to_screen_name" : null ,
"user" :{
"id" : 165913533 ,
"id_str" : "165913533" ,
"name" : "James Mackenzie" ,
"screen_name" : "jamesfmackenzie" ,
"location" : "London" ,
"profile_location" : null ,
"description" : "Science, Computers, Programming. All good." ,
"url" : "http: \/\/ t.co \/ GUwCHWp6xx" ,
"entities" :{
"url" :{
"urls" :[
{
"url" : "http: \/\/ t.co \/ GUwCHWp6xx" ,
"expanded_url" : "http: \/\/ www.jamesfmackenzie.com" ,
"display_url" : "jamesfmackenzie.com" ,
"indices" :[
0 ,
22
]
}
]
},
"description" :{
"urls" :[
]
}
},
"protected" : false ,
"followers_count" : 733 ,
"friends_count" : 1375 ,
"listed_count" : 31 ,
"created_at" : "Mon Jul 12 21:36:16 +0000 2010" ,
"favourites_count" : 148 ,
"utc_offset" : 0 ,
"time_zone" : "London" ,
"geo_enabled" : true ,
"verified" : false ,
"statuses_count" : 1649 ,
"lang" : "en" ,
"contributors_enabled" : false ,
"is_translator" : false ,
"is_translation_enabled" : false ,
"profile_background_color" : "666666" ,
"profile_background_image_url" : "http: \/\/ pbs.twimg.com \/ profile_background_images \/ 668567178 \/ 9627556962007cb48e3344e03dd8a374.png" ,
"profile_background_image_url_https" : "https: \/\/ pbs.twimg.com \/ profile_background_images \/ 668567178 \/ 9627556962007cb48e3344e03dd8a374.png" ,
"profile_background_tile" : true ,
"profile_image_url" : "http: \/\/ pbs.twimg.com \/ profile_images \/ 531419563422801921 \/ sdtKDzv4_normal.png" ,
"profile_image_url_https" : "https: \/\/ pbs.twimg.com \/ profile_images \/ 531419563422801921 \/ sdtKDzv4_normal.png" ,
"profile_link_color" : "444444" ,
"profile_sidebar_border_color" : "FFFFFF" ,
"profile_sidebar_fill_color" : "DDEEF6" ,
"profile_text_color" : "333333" ,
"profile_use_background_image" : false ,
"default_profile" : false ,
"default_profile_image" : false ,
"following" : false ,
"follow_request_sent" : false ,
"notifications" : false
},
"geo" : null ,
"coordinates" : null ,
"place" : null ,
"contributors" : null ,
"retweet_count" : 0 ,
"favorite_count" : 0 ,
"entities" :{
"hashtags" :[
{
"text" : "4KBD" ,
"indices" :[
15 ,
20
]
}
],
"symbols" :[
],
"user_mentions" :[
],
"urls" :[
{
"url" : "http: \/\/ t.co \/ hiQYV94nxg" ,
"expanded_url" : "http: \/\/ cnet.co \/ 1MNZmOo" ,
"display_url" : "cnet.co \/ 1MNZmOo" ,
"indices" :[
67 ,
89
]
}
]
},
"favorited" : false ,
"retweeted" : false ,
"possibly_sensitive" : false ,
"lang" : "en"
},
{
"created_at" : "Mon Feb 23 18:12:01 +0000 2015" ,
"id" : 569922641697640448 ,
"id_str" : "569922641697640448" ,
"text" : "Made me LOL because it's true. \" vi has two modes \u 2013 'beep repeatedly' and 'break everything' \" http: \/\/ t.co \/ NauklKGvpi" ,
"source" : " \u 003ca href= \" http: \/\/ bufferapp.com \" rel= \" nofollow \"\u 003eBuffer \u 003c \/ a \u 003e" ,
"truncated" : false ,
"in_reply_to_status_id" : null ,
"in_reply_to_status_id_str" : null ,
"in_reply_to_user_id" : null ,
"in_reply_to_user_id_str" : null ,
"in_reply_to_screen_name" : null ,
"user" :{
"id" : 165913533 ,
"id_str" : "165913533" ,
"name" : "James Mackenzie" ,
"screen_name" : "jamesfmackenzie" ,
"location" : "London" ,
"profile_location" : null ,
"description" : "Science, Computers, Programming. All good." ,
"url" : "http: \/\/ t.co \/ GUwCHWp6xx" ,
"entities" :{
"url" :{
"urls" :[
{
"url" : "http: \/\/ t.co \/ GUwCHWp6xx" ,
"expanded_url" : "http: \/\/ www.jamesfmackenzie.com" ,
"display_url" : "jamesfmackenzie.com" ,
"indices" :[
0 ,
22
]
}
]
},
"description" :{
"urls" :[
]
}
},
"protected" : false ,
"followers_count" : 733 ,
"friends_count" : 1375 ,
"listed_count" : 31 ,
"created_at" : "Mon Jul 12 21:36:16 +0000 2010" ,
"favourites_count" : 148 ,
"utc_offset" : 0 ,
"time_zone" : "London" ,
"geo_enabled" : true ,
"verified" : false ,
"statuses_count" : 1649 ,
"lang" : "en" ,
"contributors_enabled" : false ,
"is_translator" : false ,
"is_translation_enabled" : false ,
"profile_background_color" : "666666" ,
"profile_background_image_url" : "http: \/\/ pbs.twimg.com \/ profile_background_images \/ 668567178 \/ 9627556962007cb48e3344e03dd8a374.png" ,
"profile_background_image_url_https" : "https: \/\/ pbs.twimg.com \/ profile_background_images \/ 668567178 \/ 9627556962007cb48e3344e03dd8a374.png" ,
"profile_background_tile" : true ,
"profile_image_url" : "http: \/\/ pbs.twimg.com \/ profile_images \/ 531419563422801921 \/ sdtKDzv4_normal.png" ,
"profile_image_url_https" : "https: \/\/ pbs.twimg.com \/ profile_images \/ 531419563422801921 \/ sdtKDzv4_normal.png" ,
"profile_link_color" : "444444" ,
"profile_sidebar_border_color" : "FFFFFF" ,
"profile_sidebar_fill_color" : "DDEEF6" ,
"profile_text_color" : "333333" ,
"profile_use_background_image" : false ,
"default_profile" : false ,
"default_profile_image" : false ,
"following" : false ,
"follow_request_sent" : false ,
"notifications" : false
},
"geo" : null ,
"coordinates" : null ,
"place" : null ,
"contributors" : null ,
"retweet_count" : 0 ,
"favorite_count" : 0 ,
"entities" :{
"hashtags" :[
],
"symbols" :[
],
"user_mentions" :[
],
"urls" :[
{
"url" : "http: \/\/ t.co \/ NauklKGvpi" ,
"expanded_url" : "http: \/\/ bit.ly \/ 1CzDdh7" ,
"display_url" : "bit.ly \/ 1CzDdh7" ,
"indices" :[
93 ,
115
]
}
]
},
"favorited" : false ,
"retweeted" : false ,
"possibly_sensitive" : false ,
"lang" : "en"
},
{
"created_at" : "Sun Feb 22 18:12:17 +0000 2015" ,
"id" : 569560320810852353 ,
"id_str" : "569560320810852353" ,
"text" : "A really nice, structured collection of #AngularJS training resources. \" A Better Way to Learn AngularJS \" http: \/\/ t.co \/ utQMj52ZyZ" ,
"source" : " \u 003ca href= \" http: \/\/ bufferapp.com \" rel= \" nofollow \"\u 003eBuffer \u 003c \/ a \u 003e" ,
"truncated" : false ,
"in_reply_to_status_id" : null ,
"in_reply_to_status_id_str" : null ,
"in_reply_to_user_id" : null ,
"in_reply_to_user_id_str" : null ,
"in_reply_to_screen_name" : null ,
"user" :{
"id" : 165913533 ,
"id_str" : "165913533" ,
"name" : "James Mackenzie" ,
"screen_name" : "jamesfmackenzie" ,
"location" : "London" ,
"profile_location" : null ,
"description" : "Science, Computers, Programming. All good." ,
"url" : "http: \/\/ t.co \/ GUwCHWp6xx" ,
"entities" :{
"url" :{
"urls" :[
{
"url" : "http: \/\/ t.co \/ GUwCHWp6xx" ,
"expanded_url" : "http: \/\/ www.jamesfmackenzie.com" ,
"display_url" : "jamesfmackenzie.com" ,
"indices" :[
0 ,
22
]
}
]
},
"description" :{
"urls" :[
]
}
},
"protected" : false ,
"followers_count" : 733 ,
"friends_count" : 1375 ,
"listed_count" : 31 ,
"created_at" : "Mon Jul 12 21:36:16 +0000 2010" ,
"favourites_count" : 148 ,
"utc_offset" : 0 ,
"time_zone" : "London" ,
"geo_enabled" : true ,
"verified" : false ,
"statuses_count" : 1649 ,
"lang" : "en" ,
"contributors_enabled" : false ,
"is_translator" : false ,
"is_translation_enabled" : false ,
"profile_background_color" : "666666" ,
"profile_background_image_url" : "http: \/\/ pbs.twimg.com \/ profile_background_images \/ 668567178 \/ 9627556962007cb48e3344e03dd8a374.png" ,
"profile_background_image_url_https" : "https: \/\/ pbs.twimg.com \/ profile_background_images \/ 668567178 \/ 9627556962007cb48e3344e03dd8a374.png" ,
"profile_background_tile" : true ,
"profile_image_url" : "http: \/\/ pbs.twimg.com \/ profile_images \/ 531419563422801921 \/ sdtKDzv4_normal.png" ,
"profile_image_url_https" : "https: \/\/ pbs.twimg.com \/ profile_images \/ 531419563422801921 \/ sdtKDzv4_normal.png" ,
"profile_link_color" : "444444" ,
"profile_sidebar_border_color" : "FFFFFF" ,
"profile_sidebar_fill_color" : "DDEEF6" ,
"profile_text_color" : "333333" ,
"profile_use_background_image" : false ,
"default_profile" : false ,
"default_profile_image" : false ,
"following" : false ,
"follow_request_sent" : false ,
"notifications" : false
},
"geo" : null ,
"coordinates" : null ,
"place" : null ,
"contributors" : null ,
"retweet_count" : 1 ,
"favorite_count" : 2 ,
"entities" :{
"hashtags" :[
{
"text" : "AngularJS" ,
"indices" :[
40 ,
50
]
}
],
"symbols" :[
],
"user_mentions" :[
],
"urls" :[
{
"url" : "http: \/\/ t.co \/ utQMj52ZyZ" ,
"expanded_url" : "http: \/\/ bit.ly \/ 1v47RiB" ,
"display_url" : "bit.ly \/ 1v47RiB" ,
"indices" :[
105 ,
127
]
}
]
},
"favorited" : false ,
"retweeted" : false ,
"possibly_sensitive" : false ,
"lang" : "en"
}
]
That’s it! I hope to make use of the Twitter API over the next few weeks and post something interesting/useful.
Comments