twitter_api_v2 2.1.0 copy "twitter_api_v2: ^2.1.0" to clipboard
twitter_api_v2: ^2.1.0 copied to clipboard

outdated

The lightweight and powerful wrapper library for Twitter API v2.0 written in Dart language. It works cross-platform.

twitter_api_v2

The Lightweight and Cross-Platform Wrapper for Twitter API v2.0 🐦


Awesome GitHub Sponsor

v2 pub package Dart SDK Version Test Analyzer codecov Issues Pull Requests Stars Contributors Code size Last Commits License Contributor Covenant FOSSA Status


1. Guide #

This library provides the easiest way to use Twitter API v2.0.

Show some ❤️ and star the repo to support the project.

1.1. Getting Started #

1.1.1. Install Library #

With Dart:

 dart pub add twitter_api_v2

Or With Flutter:

 flutter pub add twitter_api_v2

1.1.2. Import #

import 'package:twitter_api_v2/twitter_api_v2';

1.1.3. Implementation #

import 'package:twitter_api_v2/twitter_api_v2.dart' as v2;

void main() async {
  //! You need to get keys and tokens at https://developer.twitter.com
  final twitter = v2.TwitterApi(
    //! Authentication with OAuth2.0 is the default.
    //!
    //! Note that to use endpoints that require certain user permissions,
    //! such as Tweets and Likes, you need a token issued by OAuth2.0 PKCE.
    bearerToken: 'YOUR_TOKEN_HERE',

    //! Or perhaps you would prefer to use the good old OAuth1.0a method
    //! over the OAuth2.0 PKCE method. Then you can use the following code
    //! to set the OAuth1.0a tokens.
    //!
    //! However, note that some endpoints cannot be used for OAuth 1.0a method
    //! authentication.
    oauthTokens: v2.OAuthTokens(
      consumerKey: 'YOUR_CONSUMER_KEY_HERE',
      consumerSecret: 'YOUR_CONSUMER_SECRET_HERE',
      accessToken: 'YOUR_ACCESS_TOKEN_HERE',
      accessTokenSecret: 'YOUR_ACCESS_TOKEN_SECRET_HERE',
    ),
  );

  try {
    // Get the authenticated user's profile.
    final me = await twitter.usersService.lookupMe();
    // Get the tweets associated with the search query.
    final tweets = await twitter.tweetsService.searchRecent(
      query: '#ElonMusk',
      maxResults: 20,
      // You can expand the search result.
      expansions: [
        v2.TweetExpansion.authorId,
        v2.TweetExpansion.inReplyToUserId,
      ],
    );

    await twitter.tweetsService.createLike(
      userId: me.data.id,
      tweetId: tweets.data.first.id,
    );

    // High-performance Stream endpoints are available.
    final stream = await twitter.tweetsService.connectVolumeStreams();
    await for (final tweet in stream.handleError(print)) {
      print(tweet);
    }
  } on v2.TwitterException catch (e) {
    print(e.response.headers);
    print(e.body);
    print(e);
  }
}

1.2. Supported Endpoints #

1.2.1. Tweets Service #

  1. Manage Tweet
    1. DELETE /2/tweets/:id
    2. POST /2/tweets
  2. Likes
    1. DELETE /2/users/:id/likes/:tweet_id
    2. GET /2/tweets/:id/liking_users
    3. GET /2/users/:id/liked_tweets
    4. POST /2/users/:id/likes
  3. Retweets
    1. DELETE /2/users/:id/retweets/:source_tweet_id
    2. GET /2/tweets/:id/retweeted_by
    3. POST /2/users/:id/retweets
  4. Quote Tweets
    1. GET /2/tweets/:id/quote_tweets
  5. Search Tweets
    1. GET /2/tweets/search/all
    2. GET /2/tweets/search/recent
  6. Lookup Tweets
    1. GET /2/tweets
    2. GET /2/tweets/:id
  7. Tweet Counts
    1. GET /2/tweets/counts/all
    2. GET /2/tweets/counts/recent
  8. Bookmarks
    1. GET /2/tweets/counts/all
    2. GET /2/tweets/counts/recent
  9. Timelines
    1. GET /2/users/:id/mentions
    2. GET /2/users/:id/tweets
  10. Volume Streams
    1. GET /2/tweets/sample/stream

1.2.2. Users Service #

  1. Follows
    1. DELETE /2/users/:source_user_id/following/:target_user_id
    2. GET /2/users/:id/followers
    3. GET /2/users/:id/following
    4. POST /2/users/:id/following
  2. Lookup Users
    1. GET /2/users
    2. GET /2/users/:id
    3. GET /2/users/by
    4. GET /2/users/by/username/:username
    5. GET /2/users/me
  3. Users Mutes
    1. DELETE /2/users/:source_user_id/muting/:target_user_id
    2. GET /2/users/:id/muting
    3. POST /2/users/:id/muting
  4. Hide Replies
    1. PUT /2/tweets/:id/hidden
  5. Blocks
    1. DELETE /2/users/:source_user_id/blocking/:target_user_id
    2. GET /2/users/:id/blocking
    3. POST /2/users/:id/blocking

1.2.3. Spaces Service #

  1. Search Spaces
    1. GET /2/spaces/search
  2. Lookup Spaces
    1. GET /2/spaces
    2. GET /2/spaces/:id
    3. GET /2/spaces/:id/buyers
    4. GET /2/spaces/:id/tweets
    5. GET /2/spaces/by/creator_ids

1.2.4. Lists Service #

  1. Lookup Lists
    1. GET /2/lists/:id
    2. GET /2/users/:id/owned_lists
  2. Pinnings
    1. DELETE /2/users/:id/pinned_lists/:list_id
    2. GET /2/users/:id/pinned_lists
    3. POST /2/users/:id/pinned_lists
  3. Tweet Lookup
    1. GET /2/lists/:id/tweets
  4. Manage
    1. DELETE /2/lists/:id
    2. PUT /2/lists/:id
    3. POST /2/lists
  5. Follows
    1. DELETE /2/users/:id/followed_lists/:list_id
    2. GET /2/lists/:id/followers
    3. GET /2/users/:id/followed_lists
    4. POST /2/users/:id/followed_lists
  6. Members
    1. DELETE /2/lists/:id/members/:user_id
    2. GET /2/lists/:id/members
    3. GET /2/users/:id/list_memberships
    4. POST /2/lists/:id/members

1.2.5. Compliance Service #

  1. Batch Compliance
    1. GET /2/compliance/jobs
    2. GET /2/compliance/jobs/:id
    3. POST /2/compliance/jobs

Note
Not all additional fields listed in the official documentation are supported. We intend to support them step by step.
Also you can create an Issue or Pull Request if you wish to suggest or contribute!

1.3. Tips #

1.3.1. Method Names #

twitter_api_v2 uses the following standard prefixes depending on endpoint characteristics. It's very easy to use twitter_api_v2 to find the method corresponding to the endpoint you want to use!

  • lookup
    • This prefix is attached to the endpoint performing the search.
    • However, it's distinguished from the higher-performance Search endpoint.
  • create
    • This prefix is attached to the endpoint performing the create state such as Tweet and Follow.
  • destroy
    • This prefix is attached to the endpoint performing the destroy state such as Tweet and Follow.
  • search
    • This prefix is attached to high-performance Search endpoints.
  • count
    • This prefix is attached to the endpoint that counts tweets, etc.
  • update
    • This prefix is attached to the endpoint performing the update state.
  • connect
    • This prefix is attached to the endpoint performing the high-performance streaming.

1.3.2. Generate App-Only Bearer Token #

twitter_api_v2 provides utility to generate/find your app-only bearer token.

import 'package:twitter_api_v2/twitter_api_v2.dart' as v2;

void main() async {
  final bearerToken = await v2.OAuthUtils.generateAppOnlyBearerToken(
    consumerKey: 'YOUR_CONSUMER_KEY',
    consumerSecret: 'YOUR_CONSUMER_SECRET',
  );

  print(bearerToken);
}

1.3.3. Null Parameter at Request #

In this library, parameters that are not required at request time, i.e., optional parameters, are defined as nullable. However, developers do not need to be aware of the null parameter when sending requests when using this library.

It means the parameters specified with a null value are safely removed and ignored before the request is sent.

For example, arguments specified with null are ignored in the following request.

import 'package:twitter_api_v2/twitter_api_v2.dart' as v2;

void main() async {
  final twitter = v2.TwitterApi(bearerToken: 'YOUR_TOKEN_HERE');

  await twitter.tweetsService.createTweet(
    text: 'Hello, World!',
    // These parameters are ignored at request because they are null.
    mediaIds: null,
    expansions: null,
  );
}

1.4. Contribution #

If you would like to contribute to twitter_api_v2, please create an issue or create a Pull Request.

There are many ways to contribute to the OSS. For example, the following subjects can be considered:

  • There are request parameters or response fields that are not implemented.
  • Documentation is outdated or incomplete.
  • Have a better way or idea to achieve the functionality.
  • etc...

You can see more details from resources below:

Or you can create a discussion if you like.

Feel free to join this development, diverse opinions make software better!

1.5. Support #

The simplest way to show us your support is by giving the project a star at GitHub and Pub.dev.

You can also support this project by becoming a sponsor on GitHub:

You can also show on your repository that your app is made with twitter_api_v2 by using one of the following badges:

Powered by twitter_api_v2 Powered by twitter_api_v2 Powered by twitter_api_v2

[![Powered by twitter_api_v2](https://img.shields.io/badge/Powered%20by-twitter_api_v2-00acee.svg)](https://github.com/twitter-dart/twitter-api-v2)
[![Powered by twitter_api_v2](https://img.shields.io/badge/Powered%20by-twitter_api_v2-00acee.svg?style=flat-square)](https://github.com/twitter-dart/twitter-api-v2)
[![Powered by twitter_api_v2](https://img.shields.io/badge/Powered%20by-twitter_api_v2-00acee.svg?style=for-the-badge)](https://github.com/twitter-dart/twitter-api-v2)

1.6. License #

All resources of twitter_api_v2 is provided under the BSD-3 license.

FOSSA Status

Note: License notices in the source are strictly validated based on .github/header-checker-lint.yml. Please check header-checker-lint.yml for the permitted standards.

1.7. More Information #

twitter_api_v2 was designed and implemented by Kato Shinya.

71
likes
0
pub points
85%
popularity

Publisher

verified publishershinyakato.dev

The lightweight and powerful wrapper library for Twitter API v2.0 written in Dart language. It works cross-platform.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

freezed_annotation, http, json_annotation, oauth1

More

Packages that depend on twitter_api_v2