Go Down

Topic: Problems implementing Twitter's API v1.1 using OAuth on Arduino (Read 3485 times) previous topic - next topic

elHombreGer

Feb 18, 2013, 07:02 pm Last Edit: Feb 21, 2013, 12:46 pm by G3RMiCH Reason: 1
Hi,

I'm using an Arduino Uno board with the Ethernet Shield to get the last tweet (of Twitter) from a public user timeline (it's mine) and display it on the Arduino serial monitor. I want the whole device to work without Python or Ruby and as it's a stand alone project it won't be used with a computer.
I create a client that connects to api.twitter.com and I use this GET command of the REST API v1.0 to obtain it:

Code: [Select]

client.print("GET /1/statuses/user_timeline.json?&screen_name=tuitduino&count=1 HTTP/1.1");
client.println("HOST: api.twitter.com");
client.println();


It works properly, but according to the Twitter's API documentation (https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline), I must change my GET command to the new version 1.1. This is what I do:

Code: [Select]

client.print("GET /1.1/statuses/user_timeline.json?&screen_name=tuitduino&count=1 HTTP/1.1");
client.println("HOST: api.twitter.com");
client.println();


but it doesn't work. According to the Twitter's API documentation, I must use OAuth to access this information, that is, to authenticate and authorize an aplication, wich I must create (in my case I suppose that it have to be a website), to read or write tweets from a user account (mine's) on behalf of it. The problem is that I don't know how to integrate the GET HTTP request and the OAuth information with my Arduino code. I've seen the Twitter's API documentation in depth and there are a few examples and libraries to do it "easily" but they are written for other programming languages, not for Arduino.
According to the documentation, I must implement a header, something like this:

Quote

Authorization: OAuth oauth_consumer_key="cCB46H0y6R8Hrx836WwAA", oauth_nonce="18106fa9ce0b1083367cfbd50720abea", oauth_signature="XXXXXXXXXXXXXXXXXXXXXXX", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1361444134", oauth_token="1004705586-tF4876Qu6qTwLMuTaCjbZCLTbFt4hDoRoCun3qW", oauth_version="1.0"


where the alphanumeric values are given to me from Twitter and XXXXXXXXXXXXXXXXXXXXXXX is a private value so I omitted it.

Does anybody has some idea to do it with Arduino? The GET method and the header. It's very important for me because it's for my final project.

Thanks,

Ger.

danieljay

I have a fully functional setup you're describing on the Raspberry Pi. Here's a link to a demo video of it working :http://vimeo.com/61256196

Email me at: danbertner(@)gmail(dot).com if you need help and want to go in this direction.

johnwasser

It's the two newlines in a row that signals the end of headers:
Code: [Select]
client.println("HOST: api.twitter.com");
client.println();

To add more headers, change the first .println() to .print() and add a new .println() after your headers:
Code: [Select]
client.print(F("GET /1/statuses/user_timeline.json?&screen_name=tuitduino&count=1 HTTP/1.1"));
client.print(F("HOST: api.twitter.com"));
client.print(F("Authorization: OAuth oauth_consumer_key=\"cCB46H0y6R8Hrx836WwAA\", oauth_nonce=\"18106fa9ce0b1083367cfbd50720abea\", oauth_signature=\"XXXXXXXXXXXXXXXXXXXXXXX\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1361444134\", oauth_token=\"1004705586-tF4876Qu6qTwLMuTaCjbZCLTbFt4hDoRoCun3qW\", oauth_version=\"1.0\""));
client.println();
client.println();


Also note the use of backslash to put quote characters inside a string constant.

Also not the use of the F() macro to keep those long string constants from taking up room in precious SRAM.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

nizarr

hi,
i need to know how to coleck the parameters that you use in this code( autorization header);increasingly have to take those from the page developers twitter; ther is an important note ( in api page of twitter); OAuth signing results
Important: This will only be valid for a few minutes. Also remember the cURL command will actually execute the request.

so have you try realy the code on this example( i did but it doesn`t work, i had no reponse):
making HTTP request...
HTTP/1.1 400 Bad Request
Content-Length: 0
Connection: close
when i use :   client.print(F("GET /1.1/statuses/user_timeline.json?screen_name=plantaexpre&count=1 HTTP/1.1"));
   
Code: [Select]
client.print(F("Host:api.twitter.com"));
    client.print(F("Authorization: OAuth oauth_consumer_key=\"j3UCRhGsBZSfRltac4zEXg\", oauth_nonce=\"XXXXXXXXXXXXXXXXXXXXXXX\", oauth_signature=\"XXXXXXXXXXXXXXXXXXXXXX\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1364306703\", oauth_token=\"*********************************************\", oauth_version=\"1.0\""));
    //client.println("Connection:close");
    client.println();
    client.println();


have you try this code with anther  Twitter APi v1.1 ???

SurferTim

You are not sending a cr/lf with those. Use client.println() instead of .print().
Quote
when i use :   client.print(F("GET /1.1/statuses/user_timeline.json?screen_name=plantaexpre&count=1 HTTP/1.1"));


This is how the server sees your request, and this would be bad.
Code: [Select]
GET /1.1/statuses/user_timeline.json?screen_name=plantaexpre&count=1 HTTP/1.1Host:api.twitter.comAuthorization: OAuth oauth_consumer_key="j3UCRhGsBZSfRltac4zEXg", oauth_nonce="XXXXXXXXXXXXXXXXXXXXXXX", oauth_signature="XXXXXXXXXXXXXXXXXXXXXX", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1364306703", oauth_token="*********************************************", oauth_version="1.0"


nizarr

hi,

thanks for the note
i have try with println() and the reponse was the error 404 that means : the requested format is not supported by the requested method.

so ther is some help to use API v1.1 with the oauth

┬┐should i use a library for the authentication?
and how can i get nonce parametre and signature, because the values from twitter developers page  will only be valid for a few minutes?
my application should connect automatically; but with this methode i will give the application (in the arduino scketh) all the parametres it need, and the nonce parameters is only for a unique access..
any help please.

ther is another post for oauth http://arduino.cc/forum/index.php/topic,156599.0.html

SurferTim

Error 404 means "file not found". According to your code, this one:
api.twitter.com/1.1/statuses/user_timeline.json

Go Up