twitter

Twitter connection object

Description

The twitter function creates a twitter object, which represents a Twitter® connection.

To establish the connection, you must obtain these required credentials from Twitter:

  • Consumer key

  • Consumer secret

  • Access token

  • Access token secret

To obtain these credentials, you must first log in to your Twitter account. Then, fill out the form in Create an application.

After you create a twitter object, you can use the object functions to retrieve historical Twitter data with a Twitter connection. Or, you can retrieve other Twitter data by using the Twitter REST API to access other REST API endpoints.

Creation

Description

example

c = twitter(consumerkey,consumersecret,accesstoken,accesstokensecret) creates a Twitter connection using the consumer key, consumer secret, access token, and access token secret.

Input Arguments

expand all

Consumer key (API key), specified as a character vector or string scalar. To obtain your consumer key, fill out the form in Create an application.

Data Types: char | string

Consumer secret (API secret), specified as a character vector or string scalar. To obtain your consumer secret, fill out the form in Create an application.

Data Types: char | string

Access token, specified as a character vector or string scalar. To obtain your access token, fill out the form in Create an application.

Data Types: char | string

Access token secret, specified as a character vector or string scalar. To obtain your access token secret, fill out the form in Create an application.

Data Types: char | string

Properties

expand all

Account name in the Twitter profile, specified as a character vector.

Data Types: char

Twitter user name, specified as a character vector.

Example: @username

Data Types: char

Twitter account and profile information, specified as a structure.

After creating a Twitter connection, you can access your account and profile information using the MetaData property. For example:

c.MetaData
ans = 

  struct with fields:

                                    id: 1.234e+17
                                id_str: '123456789101112141'
                                  name: 'Full Name'
                           screen_name: 'username'
                           ...

(The values here do not represent real Twitter data.)

Data Types: struct

Connection status code, specified as a matlab.net.http.StatusCode object. When this property has the value OK, the Twitter connection is successful.

Object Functions

searchSearch for Tweets
getdataRetrieve Twitter data
postdataPost Twitter data

Examples

collapse all

Use a Twitter connection object to search for Tweets.

Create a Twitter connection using your credentials. (The values in this example do not represent real Twitter credentials.)

consumerkey = 'abcdefghijklmnop123456789';
consumersecret = 'qrstuvwxyz123456789';
accesstoken = '123456789abcdefghijklmnop';
accesstokensecret = '123456789qrstuvwxyz';

c = twitter(consumerkey,consumersecret,accesstoken,accesstokensecret);

Check the Twitter connection. If the StatusCode property has the value OK, the connection is successful.

c.StatusCode
ans = 

    OK

Search for Tweets using the Twitter connection object and the search term MathWorks.

tweetquery = 'MathWorks';
d = search(c,tweetquery)
d = 

  ResponseMessage with properties:

    StatusLine: 'HTTP/1.1 200 OK'
    StatusCode: OK
        Header: [1×25 matlab.net.http.HeaderField]
          Body: [1×1 matlab.net.http.MessageBody]
     Completed: 0

d is a matlab.net.http.ResponseMessage object. The StatusCode property shows OK, indicating a successful HTTP request.

Access MathWorks® Tweets. Display the 12th Tweet.

 d.Body.Data.statuses{12}.text
ans =

    'MATLAB Control Systems Examples https://t.co/g2P86srv33'

You can search for other Tweets using the search function. To retrieve other Twitter data, use the getdata function.

Introduced in R2017b