Skip to content

Instantly share code, notes, and snippets.

@satomacoto
Forked from apolloclark/Twitter API with Curl
Created September 9, 2017 01:12
Show Gist options
  • Save satomacoto/5571137b64a0f87d0122fb86741773c7 to your computer and use it in GitHub Desktop.
Save satomacoto/5571137b64a0f87d0122fb86741773c7 to your computer and use it in GitHub Desktop.
Twitter API with Curl
# create an account, create an app
# @see https://apps.twitter.com/
# retrieve the access tokens
# @see https://dev.twitter.com/oauth/reference/post/oauth2/token
# create the file ~/twitter_api
nano ~/twitter_api
Authorization: OAuth oauth_consumer_key="XXXXXX", oauth_nonce="11111111", oauth_signature="XXXXXX", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1450728725", oauth_token="99999-XXXXXX", oauth_version="1.0"
# install JQ
# @see https://stedolan.github.io/jq/manual/
sudo apt-get install jq
# test requests
# @see https://dev.twitter.com/rest/tools/console
# example, fully written
curl
--get 'https://api.twitter.com/1.1/search/tweets.json'
--data '&q=%23archaeology'
--header ''Authorization: OAuth oauth_consumer_key="AAAA", oauth_nonce="9999", oauth_signature="AAAA", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1450742465", oauth_token="9999-AAAA", oauth_version="1.0"'
--silent
# example, loading auth headers from file
curl
--get 'https://api.twitter.com/1.1/search/tweets.json'
--data 'q=%23archaeology'
--header "$(cat ~/twitter_api)"
--silent
# example, using option short-hand
curl
-G 'https://api.twitter.com/1.1/search/tweets.json'
-d 'q=%23archaeology'
-h "$(cat ~/twitter_api)"
-s
# example, as one-liner
curl -G 'https://api.twitter.com/1.1/search/tweets.json' -d 'q=%23archaeology' -h "$(cat ~/twitter_api)" -s
# do a search with curl, pretty print with jq
# @see http://curl.haxx.se/docs/manpage.html
# @see https://dev.twitter.com/rest/public/search
curl -s -H "$(cat ~/twitter_api)" -d 'q=%40twitterapi' -G 'https://api.twitter.com/1.1/search/tweets.json' | jq '.'
# do a search, print tweet text
curl -s -H "$(cat ~/twitter_api)" -d 'q=%40twitterapi' -G 'https://api.twitter.com/1.1/search/tweets.json' | jq '.statuses[].text'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment