TwitterApi class abstract

This class represents Twitter API v2.0.

Supported Services

Authentication

An access token generated by Twitter Developer is required to use the Twitter API v2.0 endpoint with this object.

The authentication method supports OAuth 2.0 and OAuth 1.0a. However, please note that if either the Bearer token for OAuth 2.0 or the access token for OAuth 1.0a is not specified, an ArgumentError will occur when creating an instance of the TwitterApi object.

You can easily initialize TwitterApi object like below:

With OAuth 2.0

final twitter = TwitterApi(bearerToken: 'YOUR_TOKEN_HERE');

With OAuth 1.0a

final twitter = TwitterApi(
  bearerToken: 'YOUR_TOKEN_HERE',
  oauthTokens: OAuthTokens(
    consumerKey: 'YOUR_CONSUMER_KEY_HERE',
    consumerSecret: 'YOUR_CONSUMER_SECRET_HERE',
    accessToken: 'YOUR_ACCESS_TOKEN_HERE',
    accessTokenSecret: 'YOUR_ACCESS_TOKEN_SECRET_HERE',
  ),
);

Then you can access to services and endpoints as you need.

final twitter = TwitterApi(bearerToken: 'YOUR_TOKEN_HERE');

await twitter.tweets.searchRecent(query: '#ElonMusk');

User Context

Twitter API v2.0 has a detailed user context for each endpoint, but you do not need to be aware of the user context when using TwitterApi. For example, some endpoints can authenticate with either OAuth 2.0 or OAuth 1.0a.

In such cases, this library authenticates as OAuth 2.0 when only the Bearer token is passed as an argument to the TwitterApi object, and authenticates as OAuth 1.0a when the Bearer token and OAuth 1.0a access token are passed at the same time. And endpoints that can only authenticate using the OAuth 2.0 method will authenticate using the Bearer token even if an OAuth 1.0a access token is passed.

App-Only Bearer Token and OAuth 2.0 PKCE

When authenticating with OAuth 2.0, it is necessary to distinguish between the Bearer token generated by the OAuth 2.0 PKCE and the App-Only Bearer token. App-Only bearer token does not have user-specific permissions to operate on content, and therefore cannot be used on some endpoints.

The specification for obtaining an access token with OAuth 2.0 PKCE is not provided in this library. This is because this flow must go through a web browser and requires a dependency on Flutter. Instead, we provide the following separate library that supports access token generation with OAuth 2.0 PKCE.

Timeout

When communicating with the Twitter API v2.0, network and other infrastructure conditions may inhibit communication, and resulting in inevitable timeouts.

The timeout period in this library is defined as 10 seconds, but if you wish to specify an arbitrary timeout period, the following options are available.

final twitter = TwitterApi(
  bearerToken: 'YOUR_TOKEN_HERE',
  timeout: Duration(seconds: 30),
);

Exceptions

The following exceptions may be thrown by this object.

More Information

You can see more information from the following links:

Constructors

TwitterApi({required String bearerToken, OAuthTokens? oauthTokens, Duration timeout = const Duration(seconds: 10), RetryConfig? retryConfig})
Returns the new instance of TwitterApi.
factory

Properties

compliance ComplianceService
Returns the compliance service.
no setter
complianceService ComplianceService
Returns the compliance service.
no setter
directMessages DirectMessagesService
Returns the direct messages service.
no setter
geo GeoService
Returns the geo service.
no setter
hashCode int
The hash code for this object.
no setterinherited
lists ListsService
Returns the lists service.
no setter
listsService ListsService
Returns the lists service.
no setter
media MediaService
Returns the media service
no setter
mediaService MediaService
Returns the media service
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
spaces SpacesService
Returns the spaces service.
no setter
spacesService SpacesService
Returns the spaces service.
no setter
Returns the trends service.
no setter
tweets TweetsService
Returns the tweets service.
no setter
tweetsService TweetsService
Returns the tweets service.
no setter
users UsersService
Returns the users service.
no setter
usersService UsersService
Returns the users service.
no setter

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited