twitter_sign_in 5.5.0
twitter_sign_in: ^5.5.0 copied to clipboard
A Flutter plugin that provides a cross-platform API for authenticating users with Twitter, handling OAuth flow and access tokens.
import 'package:flutter/material.dart';
import 'package:twitter_sign_in/twitter_login.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final String apiKey = 'REPLACE_YOUR_API_KEY';
final String apiSecretKey = 'REPLACE_YOUR_API_SECRET_KEY';
final String clientId = 'REPLACE_YOUR_CLIENT_ID';
final String redirectURI = 'REPLACE_YOUR_REDIRECT_URI';
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: const Text('twitter_login example app'),
),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: ListView(
children: [
const SizedBox(height: 24),
const Text(
'Twitter API v1.1 is not available when creating a new application in twitter developer from November 15, 2021.\n'
'Check the Twitter Developer to see if it supports v1.1 or v2.',
),
const SizedBox(height: 24),
Image.asset(
'assets/twitter_dashboard.png',
width: double.infinity,
),
const SizedBox(height: 24),
Center(
child: TextButton(
child: const Text('use Twitter API v1.1'),
style: ButtonStyle(
foregroundColor:
WidgetStateProperty.all<Color>(Colors.white),
backgroundColor:
WidgetStateProperty.all<Color>(Colors.blueAccent),
minimumSize:
WidgetStateProperty.all<Size>(const Size(160, 48)),
),
onPressed: () async {
await login();
},
),
),
const SizedBox(height: 24),
Center(
child: TextButton(
child: const Text('use Twitter API v2.0'),
style: ButtonStyle(
foregroundColor:
WidgetStateProperty.all<Color>(Colors.white),
backgroundColor:
WidgetStateProperty.all<Color>(Colors.blueAccent),
minimumSize:
WidgetStateProperty.all<Size>(const Size(160, 48)),
),
onPressed: () async {
await loginV2();
},
),
),
const SizedBox(height: 24),
Center(
child: TextButton(
child: const Text('Get Auth Code V2 (Manual)'),
style: ButtonStyle(
foregroundColor:
WidgetStateProperty.all<Color>(Colors.white),
backgroundColor:
WidgetStateProperty.all<Color>(Colors.green),
minimumSize:
WidgetStateProperty.all<Size>(const Size(160, 48)),
),
onPressed: () async {
await getAuthCodeV2();
},
),
),
],
),
),
),
);
}
/// Use Twitter API v1.1
Future login() async {
final twitterLogin = TwitterLogin(
/// Consumer API keys
apiKey: apiKey,
/// Consumer API Secret keys
apiSecretKey: apiSecretKey,
/// Registered Callback URLs in TwitterApp
/// Android is a deeplink
/// iOS is a URLScheme
redirectURI: 'example://',
);
/// Forces the user to enter their credentials
/// to ensure the correct users account is authorized.
/// If you want to implement Twitter account switching, set [force_login] to true
/// login(forceLogin: true);
final authResult = await twitterLogin.login();
switch (authResult.status) {
case TwitterLoginStatus.loggedIn:
// success
print('====== Login success ======');
print(authResult.authToken);
print(authResult.authTokenSecret);
break;
case TwitterLoginStatus.cancelledByUser:
// cancel
print('====== Login cancel ======');
break;
case TwitterLoginStatus.error:
case null:
// error
print('====== Login error ======');
break;
}
}
/// Use Twitter API v2.
Future loginV2() async {
final twitterLogin = TwitterLogin(
/// Consumer API keys
apiKey: apiKey,
/// Consumer API Secret keys
apiSecretKey: apiSecretKey,
/// Registered Callback URLs in TwitterApp
/// Android is a deeplink
/// iOS is a URLScheme
redirectURI: 'example://',
);
/// Forces the user to enter their credentials
/// to ensure the correct users account is authorized.
/// If you want to implement Twitter account switching, set [force_login] to true
/// login(forceLogin: true);
final authResult = await twitterLogin.loginV2(
clientId: clientId,
);
switch (authResult.status) {
case TwitterLoginStatus.loggedIn:
// success
print('====== Login success ======');
break;
case TwitterLoginStatus.cancelledByUser:
// cancel
print('====== Login cancel ======');
break;
case TwitterLoginStatus.error:
case null:
// error
print('====== Login error ======');
break;
}
}
/// Get only the Auth Code (V2) for manual handling
Future getAuthCodeV2() async {
final twitterLogin = TwitterLogin(
apiKey: apiKey,
apiSecretKey: apiSecretKey,
redirectURI: redirectURI,
);
try {
final authResult = await twitterLogin.getAuthorizationCode(
clientId: clientId,
);
print('====== Auth Code Success ======');
print('Code: ${authResult.code}');
print('Verifier: ${authResult.codeVerifier}');
// You can now send these to your backend to exchange for tokens
} catch (e) {
print('====== Auth Code Error ======');
print(e);
}
}
}