Search code examples
curltwittercmdget

How to search tweets in Twitter API from data endpoint in dev environment using cURL?


I want to search some tweets and GET some data from twitter API using dev environment and data endpoint. I produced consumer_key, consumer_secret, access_token, and access_token_secret. Now, I want to know How can I send a request to twitter API using cURL in cmd. When I try the following query, I get an error{bad Authentication}. Please help me if you know how to write correct cURL request for accessing twitter dev and searching tweets in cmd. Thanks for all answers. here is my cURL request:

curl --trace outputFile.txt "https://api.twitter.com/1.1/tweets/search/30day/my_dev_env_label.json?query=TwitterDev%20%5C%22search%20api%5C%22&maxResults=100" {"Authorization":"OAuth","consumer_key":"XXXXXXXXXXXXXXXXXXXXXXXXX","consumer_secret":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","access_token":"XXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","access_token_secret":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"}

I have tried to write it like some existing examples here: https://developer.twitter.com/en/docs/tweets/search/api-reference/premium-search


Solution

  • Finally I found the answer. I decided to publish my answer because I think it might be helpful for some one in the future! For sending a request to twitter API using cURL, the first step is generating Bearer token by having API(consumer) key and API secret key like the following:

    curl -u "REPLACE_YOUR_API_KEY:REPLACE_YOUR_API_SECRET_KEY" --data "grant_type=client_credentials" "https://api.twitter.com/oauth2/token"
    

    Then you'll receive a bearer token. by using this, replace your own parameters with capital words in the following request:

    curl "https://api.twitter.com/1.1/tweets/search/PRODUCT/LABEL.json?query=QUERY_URL&maxResults=100&fromDate=YYYYMMDDHHMM&toDate=YYYYMMDDHHMM" -H "Authorization: Bearer YOUR_BEARER_TOKEN"
    

    In the above request, YYYYMMDDHHMM is a date format like: 201907291530 So, It finished successfully!