twitty is a MATLAB class for interaction with the twitter platform via its REST API v1.1.
Here are some examples of its usage:
tw = twitty(credentials); % create a twitty object and supply user credentials.
For details on credentials, type "help twitty" and refer to the sections about PROPERTIES and OBTAINING TWITTER CREDENTIALS.
After setting valid credentials, a twitty object should be able to access the Twitter platform. E.g.:
1. S = tw.search('matlab'); % search twitter.com for messages including the word 'matlab'.
2. S = tw.updateStatus('Hello, Twitter!'); % or twit something cooler.
3. S = tw.sampleStatuses(); % get a continuous stream of a random sample of all public statuses.
4. S = tw.userTimeline('screen_name', 'matlab'); % get recent messages posted by the user 'matlab';
5. S = tw.trendsAvailable(); % get place for which Twitter has trends available.
Conceptually, twitty methods are just wrapper functions around the main function which calls the Twitter API. This API caller function, callTwitterAPI(), does the main job:
creates an HTTPS request, handles the authentication and encoding, sends the request to and parses a response from the Twitter platform. It is not meant to be called directly but should be invoked
from a wrapper function.
The wrapper functions provide an intuitive MATLAB-style interface for accessing the Twitter's API resources. For the complete description of the REST API v1.1, refer to the official
documentation at https://dev.twitter.com/docs/api/1.1.
The API is quite extensive and twitty doesn't cover it all. It includes most of the resources in the following sections: TIMELINES, STREAMING, TWEETS, SEARCH, FRIENDS & FOLLOWERS, USERS, PLACES & GEO, TRENDS, and HELP.
For the summary of the twitty's methods run the 'twitty.API' command.
For information on a particular function, type 'help twitty.<function name>'.
REQUIREMENTS:
optional - the json parser by Joel Feenstra: http://www.mathworks.co.uk/matlabcentral/fileexchange/20565-json-parser
1.5.0.0 | Added auxiliary files: tweetingSummary.m and coastline.mat. |
|
1.4.0.0 | Fixed: error in the constructor call twitty(creds), due to the outdated credentials validity check. |
|
1.3.0.0 | Updated to the Twitter REST API v1.1.
|
|
1.1.0.0 | Some documentation updates.
|
Inspired by: Update twitter status, JSON Parser
Inspired: Core_twitter: A wrapper for the Python package Tweepy to interface with the Twitter API
Create scripts with code, output, and formatted text in a single executable document.
Amir Mitchell (view profile)
I encountered a critical issue with the functions that return user ids (as friendsIds and followersIds). The string returned by the Twitter API contains id that are longer than 16 digits. When these are parsed by the json decoder (either json_parser or the build in jsondecode) they are *distorted* since the precision for floating point data is limited (https://www.mathworks.com/help/matlab/matlab_prog/floating-point-numbers.html)
Here is an example (fails for the first id, works for the second):
str = '{"ids":[755159767898945025,636777713],"next_cursor" 0,"next_cursor_str":"0","previous_cursor":0,"previous_cursor_str":"0"}'
value = jsondecode(str)
int2str(value.ids(1))
int2str(value.ids(1)) % should be 755159767898945025 but is 755159767898945024 on my MacBook
int2str(value.ids(2)) % should be 636777713 and is correct
Does anyone have a solution that doesn’t require rewriting the json parser or changing the twisty code?
PS - Vladimir, thanks you very much for depositing this code
Filip Riccio (view profile)
I was having the exact same issue as Rucci had on the 26 Jul 2012. My fix for this was to comment out lines: 399, 400, 401, 402, 403. That seems to work for me.
Anselm Griffin (view profile)
Hi All. I ran the same code in release 2017b that I used in release 2017a (Could not get it to work). All worked well. Why? I don't know .
Dylan Lim (view profile)
Anyone else getting the same error and if so , is there a solution ??
Error using jsondecode
JSON text must be a character vector.
Anselm Griffin (view profile)
I like many others could not save my credentials. The error that Enrcio De Santis got on Feb 7 2017 kept occurring again and again. I tried his fix dated Feb 8. This was on a windows 10 laptop. I tried again on a Windows 8 laptop. Worked first time. I dont know why but it seems to be a Java issue (obviously)
Turgis Fabien (view profile)
Hello, twitty works fine and i can post a new status in my twitter but how can i post a new status with a picture or a gif or a video attached to my tweet and add linked account to this picture ?
ellis (view profile)
Hello,
I'm trying tw = twitty(credentials), but receive error, "The supplied credentials are not valid."
i have also created twitter developer app successfully but i still have error.
Can anyone help me please?
Frank-Michael (view profile)
Enrico De Santis (view profile)
I found where is my issue in the post below related to the 401 error using streaming APIs. Now twitty works well. If there aren't other authentication issues, a problem can arise if the time measured by your machine (UNIX time) is different from the time measured on twitter streaming servers. Be sure that your time is set to be synchronized automatically through an internet timing server (SNTP) ('set time automatically" in Win7/10 settings).
Enrico De Santis (view profile)
Hello, this tool is fantastic! However I'm trying to use the streaming API and I'm getting the following error (the authentication through the standard API (e.g. tw.search...)) works well with no errors):
Error using twitty/callTwitterAPI (line 1974)
Java exception occurred:
java.io.IOException: Server returned HTTP response code: 401 for URL:
https://stream.twitter.com/1.1/statuses/filter.json
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
Error in twitty/filterStatuses (line 807)
S = twtr.callTwitterAPI(httpMethod,url,params,1);
Error in Twitter_Stream (line 44)
tw.filterStatuses('track',uk_trends{1}); % Streaming API call
Has anyone resolved this issue?
Thanks!
Abhishek Jain (view profile)
Josef Kilany (view profile)
hi can someone help me?
my credentials (line298) are not valid. I have tried to create a new app but this did not work out well
Michael C. (view profile)
Luigi Pariota (view profile)
It worked fine for me at the moment. Thank you!
Matthew Foreman (view profile)
Has anyone had any luck sending an image (.jpeg) to twitter with the twitty object?
I've tried to add methods for updateStatusWithMedia() for the Twitter API, but haven't had any luck so far.
Thanks!
sd s (view profile)
Trying tw = twitty(credentials), but receive error, "The supplied credentials are not valid."
Have created twitter developer app successfully
MATLAB 8.1.0.604 (R2013a) on Win7 x64
Any suggestions?
Kyle (view profile)
Hello Chetathorn,
I am sorry that my suggestions were of no help. I now believe my previous advice is entirely non-sense. I do not know how this code works fundamentally, but recognize the main difficulty generally experienced is a failure to authenticate with Twitter using OAuth protocols. Unfortunately, MATLAB does not have OAuth built-in.
Twitty makes a gallant effort to interface with Twitter and when it works it is awesome. However, I have found it to be rather buggy and temperamental; sometimes it works and sometimes it doesn't. Finally, I reached sufficient frustration that I developed my own solution. If you are still having trouble I would encourage you to try it for yourself, because I uploaded it to the FX.
http://www.mathworks.com/matlabcentral/fileexchange/53691
Goodluck,
Kyle
chetathorn songmuang (view profile)
Hello.
Please help on my error "The supplied credentials are not valid." I also tried method of kyle "create a credentials struct "
Still get error
Is there a solution to these errors?
I have checked my credentials on the twitter site via OAuth so I am fairly sure they are in fact valid
Kyle (view profile)
Hello Srikrishnan,
TLDR. Try using a struct to save the credentials.
I have found that the tw.saveCredentials() method that prompts the user for 4 keys in a pop-up window does not work for me. I get the same error (line 1974) that many others have.
However, if I create a credentials struct and create the 4 fields for each key from the command line with each key as a char, then I have success. Please try this approach. If you type 'help twitty' this is the first option to obtain credentials with twitty.
Srikrishnan Subramanian (view profile)
i'm getting the same errors as Sandor Toth and sushma sharma.
Is there a solution to these errors?
Srikrishnan Subramanian (view profile)
i'm getting the same errors as Sandor Toth and sushma sharma.
Is there a solution to these errors?
Kyle (view profile)
Hello,
I have a comment and a question.
First, I've noticed a number of comments about authentication errors. I too had this problem. The solution for me was to recreate the AccessToken on the twitter-dev website, after I had changed the permissions to Read and Write. The token must be regenerated to reflect the permission change.
Second, does anyone know how to send emoji with twitty?
Thanks,
Kyle
sushma sharma (view profile)
Having the same problem: "Error using twitty (line 298)
The supplied credentials are not valid."
My credentials work using OAuth...
Any help would be appreciated!
S
Sandor Toth (view profile)
I get the same error message as Mircea:
Error using twitty/callTwitterAPI (line 1974)
Java exception occurred:
java.io.IOException: Server returned HTTP response code: 401 for URL:
https://api.twitter.com/1.1/statuses/update.json
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1459)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
Error in twitty/updateStatus (line 841)
S = twtr.callTwitterAPI(httpMethod,url,params,1);
Is there any solution?
vicente torres (view profile)
I got a msg of error:
??? Error using ==> twitty
Error: File: twitty.m Line: 448 Column: 33
Unexpected MATLAB operator.
I am using Matlab 7.8.
Can you help me? Thx
Frederik Kratzert (view profile)
unfortunally i am facing the same problems as below:
Error using twitty (line 298)
The supplied credentials are not valid.
i made pretty sure, that i entered everything correctly.
Would appreciate to get some feedback, because i want to use this awesom function!
p.s. Using Matlab 2013a x64 with win 8
Mircea Anghel (view profile)
Hello.. I'm facing this error with tw.search('matlab'):
amazon = tw.search('matlab');
Error using twitty/callTwitterAPI (line 1975)
Java exception occurred:
java.io.IOException: Server returned HTTP response code: 401 for URL: https://api.twitter.com/1.1/search/tweets.json?q=matlab
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
Error in twitty/search (line 1065)
S = twtr.callTwitterAPI(httpMethod,url,params,1);
Any ideas How i can solve this?
Thanks
Sujit Sahoo (view profile)
I am also facing the same problem!
"Error using twitty (line 298)
The supplied credentials are not valid."
Justin Baacke (view profile)
Justin Baacke (view profile)
I am having the same problem as Sean Mc was having with the
"Error using twitty (line 298)
The supplied credentials are not valid."
I was wondering what you worked out to fix it. I have checked my credentials on the twitter site via OAuth so I am fairly sure they are in fact valid but I was wondering if you had any other ideas as to where the problem might lie.
Alfonso (view profile)
This looks great!! I get the same error as SosoMed. I have not been able to make it work I am new to all this, sorry. Any advise??
Thanks in advance
SosoMed (view profile)
Trying "S = tw.search('matlab'); ",
But receive error:
">> try1
Error using twitty/callTwitterAPI (line 1974)
Java exception occurred:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path
building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to
find valid certification path to requested target
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown
Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid
certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
at sun.security.validator.Validator.validate(Unknown Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(Unknown Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown
Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown
Source)
... 12 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find
valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
at java.security.cert.CertPathBuilder.build(Unknown Source)
... 18 more
Error in twitty/accountVerifyCredentials (line 1469)
S = twtr.callTwitterAPI(httpMethod,url,params,1);
Error in twitty/saveCredentials (line 398)
S = twtr.accountVerifyCredentials;
Error in try1 (line 3)
tw.saveCredentials();
>> try2
Error using twitty/callTwitterAPI (line 1974)
Java exception occurred:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX
path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable
to find valid certification path to requested target
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown
Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown
Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid
certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
at sun.security.validator.Validator.validate(Unknown Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(Unknown Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown
Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown
Source)
... 12 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find
valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
at java.security.cert.CertPathBuilder.build(Unknown Source)
... 18 more
Error in twitty/search (line 1063)
S = twtr.callTwitterAPI(httpMethod,url,params,1);
Error in try2 (line 1)
S = tw.search('matlab');
Sean Mc (view profile)
Good stuff! And thanks for the debugging help!
Sean Mc (view profile)
Trying tw = twitty(credentials), but receive error, "The supplied credentials are not valid."
Have created twitter developer app successfully (tried twice).
MATLAB 8.1.0.604 (R2013a) on Win7 x64
Any suggestions?
Arun Babu (view profile)
Can anyone explain the procedure to get & include the "VeriSign Class 3 Public Primary Certification Authority - G5" certificate?
Vladimir Bondarenko (view profile)
From the 10th of Dec, api.twitter.com is using a new SSL certificate: https://dev.twitter.com/blog/rest-api-ssl-certificate-updates
Now, to establish a secure connection to Twitter, MATLAB must have in its keystore of trusted authorities the "VeriSign Class 3 Public Primary Certification Authority - G5" certificate.
JJ (view profile)
Works again in R2013b. Reason is the updated JVM (1.6 => 1.7) handling the TLS correctly.
I'm using http://www.mathworks.com/matlabcentral/fileexchange/33381-jsonlab-a-toolbox-to-encodedecode-json-files-in-matlaboctave as JSON parser, works flawlessly so far.
Craig (view profile)
This package has worked fantastic for me for automatic data updates. Thanks!
Recently I have had the same problem as Jothi below with the ERROR:
??? Java exception occurred:
java.io.IOException: Server returned HTTP response code: 400 for URL:
http://api.twitter.com/1/account/verify_credentials.json
Any thoughts?
Thanks in advance,
Craig
Trevor Cox (view profile)
Code was just what I needed, thanks
"An output function can a usual m-file, e.g., the "tweetingSummary.m" file provided with the twitty"
Could you make tweetingSummary.m available to download?
Saurabh Jha (view profile)
it is not accepting query with or / and
for ex 'exascale or petascale'
Although twitter api supports this
Can some one help me on this?
Jothi (view profile)
Sir,
i have obtain twitter credential according to you instruction. ie.,
tw.saveCredentials()
but i have following error. how to set the credentials. how to search and download tweets using matlab.
??? Java exception occurred:
java.io.IOException: Server returned HTTP response code: 400 for URL:
http://api.twitter.com/1/account/verify_credentials.json
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
Error in ==> twitty>twitty.callTwitterAPI at 2018
inStream = BufferedReader( InputStreamReader( httpConn.getInputStream ) );
Error in ==> twitty>twitty.accountVerifyCredentials at 1410
S = twtr.callTwitterAPI(httpMethod,url,params,1);
Error in ==> twitty>twitty.saveCredentials at 233
S = twtr.accountVerifyCredentials;
kindly help me sir.
thanks in advance.
Zachary (view profile)
@Rucci
I added the following at line 1411:
if isstruct(S{1}), S = S{1}; end
Seems to fix the problem. Note: I have json_parser.m on my path.
Rucci (view profile)
Hello,
I've complete to obtain twitter credential according to your instruction.
I used my twitter credential to create the credential structure as the follows
credentials.ConsumerKey = 'XXXXXXXXXX';
credentials.ConsumerSecret = 'XXXXXXXXXXXXX';
credentials.AccessToken = 'XXXXXXXXXXXX';
credentials.AccessTokenSecret = 'XXXXXXXXXXXX';
Then I create the twitty object using the command: tw = twitty(credentials)
I got the error message as the follows
??? Error using ==> strfind
Inputs must be character arrays.
Error in ==> twitty>twitty.twitty at 149
if strfind(lower(S{1}),'error'), error('The supplied credentials are not
valid.'); end
I think the code had been modified since Apr 2012 for this problem. I'm not sure whether I did something wrong.
Would you please advise?
Jose (view profile)
I am trying to use the function friendshipsCreate and it gives me the following message...
>> S = tw.friendshipsCreate('pepe','matlab','follow','true')
??? Java exception occurred:
ice.net.URLNotFoundException: Document not found on server
at ice.net.HttpURLConnection.getInputStream(OEAB)
Error in ==> twitty>twitty.callTwitterAPI at 2018
inStream = BufferedReader( InputStreamReader(
httpConn.getInputStream ) );
Error in ==> twitty>twitty.friendshipsCreate at 1128
S =
twtr.callTwitterAPI(httpMethod,url,params,1);
Vladimir Bondarenko (view profile)
@Yu Ang Tan: thanks for the comment. I've fixed the strfind(lower(S),'error') issue.
@Justin: did it resolve your problem?
@Michael: I replied to your email, but my reply couldn't be delivered ("no such user" error). Do you have an alternative email address?
Yu Ang Tan (view profile)
Justin: change line 136:
if strfind(lower(S),'error'))...
to
if strfind(lower(S{1}),'error'))...
Michael: create a structure
credentials.ConsumerKey='SH1asd325tgdsasOVedYsA';
credentials.ConsumerSecret = 'on325gtgafafdsfegawe4365ghgSDF';
credentials.AccessToken = '123554-PZjoiso98n3ocrlWFOJ39rjlsjfwzOdq';
credentials.AccessTokenSecret = 'W6r324fjlajaMMLasffSMMk';
tw = twitty(credentials);
You'll need to replace those strings from your developer page. The strings above are just garbage values.
Michael Antaran (view profile)
Looks great. However, can you provide an example file format of how the credentials files should look like.
Sorry if this is a novice question. See potential but having difficult time using on 2007b.
thanks
Justin (view profile)
Not sure if I'm just doing something wrong but I can't seem to get this going. Whenever you try to set or save the credentials it returns an error
Conversion to logical from cell is not possible.
Beaver (view profile)
Disregard... I didn't read the twitter api suggestions. Great Code... Under promises, Over Delivers!!