twitter_api_v2 2.0.0 twitter_api_v2: ^2.0.0 copied to clipboard
The lightweight and powerful wrapper library for Twitter API v2.0 written in Dart language. It works cross-platform.
// Copyright 2022 Kato Shinya. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided the conditions.
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 {
final me = await twitter.usersService.lookupMe();
final tweets = await twitter.tweetsService.searchRecent(query: '#ElonMusk');
final response = await twitter.tweetsService.createLike(
userId: me.data.id,
tweetId: tweets.data.first.id,
);
print(response);
} on v2.TwitterException catch (e) {
print(e.response.headers);
print(e.response.body);
print(e);
}
}