Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Twitter API call through curl in unix

Tags:

curl

twitter

api

I would like to pull the data from Twitter REST API. I have created the consumer key, secret and Access token, secret. I have tried with "Test OAuth", it generates a CURL command but if I change any one parameter then it is giving the below error. Message: {"errors":[{"code":32,"message":"Could not authenticate you."}]}

Now I would like to call the twitter API using CURL in shell script for different screenNames.

I want a sample command some thing like mentioned below

curl --get 'https://api.twitter.com/1.1/statuses/user_timeline.json' --data 'count=2&screen_name=aswin' APIKEY:"xxxxxx",Acesstoken:"yyyyyyyy"

Thanks in advance.

Regards, Aswin

like image 447
user3792699 Avatar asked Aug 12 '16 07:08

user3792699


People also ask

Can we use cURL to call REST API?

curl can send all common HTTP commands to a REST API including GET , POST , PUT , and DELETE . The curl utility is straightforward to use. It has a few main options for data transmission, user authentication, and making header changes.

What is cURL Twitter?

cURL is a command-line tool for getting or sending files using the URL syntax. If you need to request a new Bearer Token, you can do so with the request below. Replace the API_KEY and API_KEY_SECRET with your Consumer API Keys from the app that you used to activate the Labs preview.


2 Answers

I found the answer.

curl --get 'https://api.twitter.com/1.1/statuses/user_timeline.json' \
     --data 'count=2&screen_name=twitterapi' \
     --header 'Authorization: OAuth oauth_consumer_key="AAAAAAAAAAAAAAAAAAAA", oauth_nonce="BBBBBBBBBBBBBBBBBBBBBBB", oauth_signature="CCCCCCCCCCCCCCCCCCCCCCCCCCC", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1471672391", oauth_token="DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD", oauth_version="1.0"'
like image 177
user3792699 Avatar answered Nov 15 '22 08:11

user3792699


Since your specific query doesn't require a user context you can use Application only authentication to make this request. The bearer token won't change per request so it should allow you to keep using curl.

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

n.b. it won't work for all endpoints, but should for the case you listed.

like image 21
Yuri Schimke Avatar answered Nov 15 '22 07:11

Yuri Schimke