Skip to content

Instantly share code, notes, and snippets.

@apolloclark
Last active September 29, 2024 09:15
Show Gist options
  • Save apolloclark/2d4f6362f31666c1c81f to your computer and use it in GitHub Desktop.
Save apolloclark/2d4f6362f31666c1c81f 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'
@jessemonroy650
Copy link

Is there a page, blog post or other instructions that point to this page? I am going to blog this, with understanding that i may move in the future. Comments?

@sudhanvadp
Copy link

What is the curl command to get fetch initial token with oauth1?
Can you or anyone help me converting the below http request to curl or fetch?

POST /oauth/request_token HTTP/1.1
User-Agent: themattharris' HTTP Client
Host: api.twitter.com
Accept: */*
Authorization:
        OAuth oauth_callback="http%3A%2F%2Flocalhost%2Fsign-in-with-twitter%2F",
              oauth_consumer_key="cChZNFj6T5R0TigYB9yd1w",
              oauth_nonce="ea9ec8429b68d6b77cd5600adbbb0456",
              oauth_signature="F1Li3tvehgcraF8DMJ7OyxO4w9Y%3D",
              oauth_signature_method="HMAC-SHA1",
              oauth_timestamp="1318467427",
              oauth_version="1.0" 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment