Flutter分享功能插件ayrshare_flutter的使用
Flutter分享功能插件ayrshare_flutter的使用
通过集成社交网络API,ayrshare_flutter
插件可以让你轻松地向多个平台发送社交媒体帖子。该插件支持Facebook、Twitter、Instagram、LinkedIn、Reddit、Telegram、TikTok、Google Business Profile(以前的Google My Business)、Pinterest和YouTube等平台。
无需自己在任何社交网络上创建开发者账户。
功能
以下是ayrshare_flutter
的一些主要功能:
- 向10个社交网络发送帖子。你可以实时发送或安排未来某个时间点发送。
- 获取缩短链接的分析数据,如分享、点赞、转发和浏览次数,以及账号数据如关注者和订阅者。
- 获取所有帖子的历史记录及其当前状态。
- 上传并存储图片和视频,并返回一个指向图片的URL。这对于没有自己的媒体托管服务的人来说非常有用。
- 获取和发送帖子评论。
- 缩短URL并返回缩短后的URL。可以跟踪点击次数。
- 添加新的RSS或Substack订阅源以自动发布所有新文章。
- 创建和管理多个用户(仅限商业计划)。可以为他们的社交账户发送帖子。
- 使用Webhooks可以在特定系统操作发生时通过调用你提供的URL通知你。
入门指南
- 开始使用免费计划或付费计划,或者如果你有一个平台或管理多个用户,请查看商业计划。
- 登录你的Ayrshare账户,连接一些社交网络,获取API密钥,并开始发送帖子。
使用示例
以下是一个简单的示例,展示如何使用ayrshare_flutter
插件向Twitter和Facebook发送随机帖子和随机图片。
import 'package:flutter/material.dart';
import 'ayrshare_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: PostingPage(),
);
}
}
class PostingPage extends StatelessWidget {
/// TODO 获取你的API密钥,通过在ayrshare.com注册获得
final apiKey = '###-###-###-###';
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () async {
await post(
apiKey: apiKey,
body: {
'randomPost': true,
'platforms': ['twitter', 'facebook'],
'randomMediaUrl': true
},
).then((value) => print(value));
},
child: const Text('Post To Social'),
),
),
);
}
}
完整示例代码
下面是一个完整的示例代码,展示了如何使用ayrshare_flutter
插件进行多种API调用。
import 'dart:convert';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:ayrshare_flutter/ayrshare_flutter.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io' as io;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.green,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
[@override](/user/override)
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
/// TODO 输入适用的数据。apiKey用于高级账户或个人用户配置文件上的商业账户。apiKeyPrimary和privateKey用于商业账户。
final apiKey = '###-###-###-###';
final apiKeyPrimary = '###-###-###-###';
final privateKey =
"-----BEGIN RSA PRIVATE KEY-----##########-----END RSA PRIVATE KEY-----\n";
final ImagePicker _picker = ImagePicker();
Future getMediaFromGallery({required String mediaType}) async {
XFile? image;
if (mediaType == 'image') {
image = await _picker.pickImage(source: ImageSource.gallery);
} else {
image = await _picker.pickVideo(source: ImageSource.gallery);
}
return image;
}
Future imageUploadToAyrshare({required fileBody}) async {
final base64Image = io.File(fileBody.path).readAsBytesSync();
String img64 = base64Encode(base64Image);
var bodyPost = {
'file': 'data:image/jpeg;base64,$img64',
// 'fileName': 'test image',
// 'description': 'best image'
};
await mediaUpload(
apiKey: apiKey,
contentType: 'application/json',
body: bodyPost,
).then((value) => log(value));
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: null,
body: Container(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 50),
child: Center(
child: Column(
children: [
const Text(
'Ayrshare',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 20),
Expanded(
child: SingleChildScrollView(
child: Wrap(
spacing: 8.0,
children: [
ElevatedButton(
onPressed: () async {
await post(
apiKey: apiKey,
body: {
'randomPost': true,
'platforms': ['twitter', 'facebook'],
'randomMediaUrl': true
},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.purple,
),
child: const Text('Post API Call'),
),
ElevatedButton(
onPressed: () async {
await delete(
apiKey: apiKey,
body: {
'id': '############',
},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
),
child: const Text('Delete API Call'),
),
ElevatedButton(
onPressed: () async {
await history(
apiKey: apiKey,
body: {
'lastDays': '10',
'lastRecords': '1',
},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.brown,
),
child: const Text('History API Call'),
),
ElevatedButton(
onPressed: () async {
await historyId(
apiKey: apiKey,
id: '############',
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.brown[300],
),
child: const Text('History ID'),
),
ElevatedButton(
onPressed: () async {
await historyPlatform(
apiKey: apiKey,
platform: 'twitter',
body: {'limit': '20'},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.brown[800],
),
child: const Text('History Platform'),
),
ElevatedButton(
onPressed: () async {
await getMediaFromGallery(mediaType: 'image').then(
(value) {
// log(value.path);
imageUploadToAyrshare(fileBody: value);
},
);
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.orange[800],
),
child: const Text('Upload image'),
),
ElevatedButton(
onPressed: () async {
mediaUploadUrl(
apiKey: apiKey,
body: {'fileName': 'filename.jpg'},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.orange[800],
),
child: const Text('Get large upload URL'),
),
ElevatedButton(
onPressed: () async {
media(
apiKey: apiKey,
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.orange,
),
child: const Text('Get all media'),
),
ElevatedButton(
onPressed: () async {
user(
apiKey: apiKey,
body: {},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
),
child: const Text('Get user profile'),
),
ElevatedButton(
onPressed: () async {
await analyticsPost(
apiKey: apiKey,
body: {
'id': '############',
'platforms': ['twitter', 'facebook']
},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
),
child: const Text('Make Analytics API Call'),
),
ElevatedButton(
onPressed: () async {
await analyticsLinks(
apiKey: apiKey,
body: {'lastDays': '10'},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.greenAccent,
),
child: const Text('Analytics Links'),
),
ElevatedButton(
onPressed: () async {
await analyticsSocial(
apiKey: apiKey,
body: {
'platforms': ['twitter', 'facebook']
},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green[800],
),
child: const Text('Analytics Social'),
),
ElevatedButton(
onPressed: () async {
await postComment(
apiKey: apiKey,
body: {
'platforms': ['twitter', 'facebook'],
'id': '############',
'comment': 'Can\'t agree more',
},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.purpleAccent,
),
child: const Text('Post Comment'),
),
ElevatedButton(
onPressed: () async {
await getComments(
apiKey: apiKey,
body: {
'id': '############',
},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.purple,
),
child: const Text('Get Comments'),
),
ElevatedButton(
onPressed: () async {
await setAutoSchedule(
apiKey: apiKey,
body: {
'schedule': ['13:05Z', '20:14Z'],
'title': 'Instagram Schedules',
},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey[800],
),
child: const Text('Set Auto Schedule'),
),
ElevatedButton(
onPressed: () async {
await deleteAutoSchedule(
apiKey: apiKey,
body: {
'title': 'Instagram Schedule',
},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey[700],
),
child: const Text('Delete Auto Schedule'),
),
ElevatedButton(
onPressed: () async {
await listAutoSchedule(
apiKey: apiKey,
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey[700],
),
child: const Text('List Auto Schedules'),
),
ElevatedButton(
onPressed: () async {
await addFeed(
apiKey: apiKey,
body: {
'url': 'https://www.ayrshare.com/feed/',
},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
),
child: const Text('Add Feed'),
),
ElevatedButton(
onPressed: () async {
await getFeeds(
apiKey: apiKey,
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue[700],
),
child: const Text('Get Feeds'),
),
ElevatedButton(
onPressed: () async {
await deleteFeed(
apiKey: apiKey,
body: {
'id': '############',
},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue[600],
),
child: const Text('Delete Feed'),
),
const Divider(
height: 20,
thickness: 2,
indent: 20,
endIndent: 0,
color: Colors.grey,
),
Container(
width: double.maxFinite,
height: 40,
alignment: Alignment.center,
child: const Text(
'Ayrshare Business Plan Only',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold,
),
),
),
ElevatedButton(
onPressed: () async {
await createProfile(
apiKey: apiKeyPrimary,
body: {
'title': 'ACME Profile A',
},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.brown,
),
child: const Text('Create Profile'),
),
ElevatedButton(
onPressed: () async {
await deleteProfile(
apiKey: apiKeyPrimary,
body: {
'profileKey': '#####-#####-#####-#####',
'title': 'ACME Profile A',
},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.brown[400],
),
child: const Text('Delete Profile'),
),
ElevatedButton(
onPressed: () async {
await updateProfile(
apiKey: apiKeyPrimary,
body: {
'profileKey': '#####-#####-#####-#####',
'title': 'ACME Profile BB',
},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.brown[600],
),
child: const Text('Update Profile'),
),
ElevatedButton(
onPressed: () async {
await getProfiles(
apiKey: apiKeyPrimary,
body: {},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.brown[400],
),
child: const Text('Get Profiles'),
),
ElevatedButton(
onPressed: () async {
await unlinkSocial(
apiKey: apiKeyPrimary,
body: {
'platform': 'twitter',
'profileKey': apiKey,
},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.brown[200],
),
child: const Text('Unlink Platform'),
),
ElevatedButton(
onPressed: () async {
await generateJWT(
apiKey: apiKeyPrimary,
body: {
'domain': '############',
'privateKey': privateKey,
'profileKey': apiKey,
'logout': true,
},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
),
child: const Text('Generate JWT & URL'),
),
ElevatedButton(
onPressed: () async {
await registerWebhook(
apiKey: apiKeyPrimary,
body: {
'action': 'social',
'url': 'https://www.ayrshare.com/webhook',
},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.orange,
),
child: const Text('Register Webhook'),
),
ElevatedButton(
onPressed: () async {
await unregisterWebhook(
apiKey: apiKeyPrimary,
body: {
'action': 'social',
},
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.orange[800],
),
child: const Text('Unregister Webhook'),
),
ElevatedButton(
onPressed: () async {
await getWebhooks(
apiKey: apiKeyPrimary,
).then((value) => log(value));
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.orange[300],
),
child: const Text('List Webhooks'),
),
],
),
),
),
],
),
),
));
}
}
更多关于Flutter分享功能插件ayrshare_flutter的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter分享功能插件ayrshare_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
ayrshare_flutter
是一个用于在 Flutter 应用中集成 Ayrshare 社交媒体分享功能的插件。Ayrshare 是一个允许开发者通过 API 将内容分享到多个社交媒体平台的服务,包括 Facebook、Twitter、Instagram、LinkedIn 等。
1. 安装插件
首先,你需要在 pubspec.yaml
文件中添加 ayrshare_flutter
插件的依赖:
dependencies:
flutter:
sdk: flutter
ayrshare_flutter: ^1.0.0 # 请检查最新版本
然后运行 flutter pub get
来安装插件。
2. 获取 Ayrshare API 密钥
在使用 ayrshare_flutter
之前,你需要在 Ayrshare 注册并获取 API 密钥。
3. 初始化插件
在你的 Flutter 应用中,初始化 ayrshare_flutter
插件。通常,你可以在 main.dart
中初始化它:
import 'package:ayrshare_flutter/ayrshare_flutter.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 Ayrshare
await AyrshareFlutter.init(apiKey: 'YOUR_API_KEY');
runApp(MyApp());
}
4. 分享内容
你可以使用 AyrshareFlutter.share
方法来分享内容到多个社交媒体平台。以下是一个简单的示例:
import 'package:flutter/material.dart';
import 'package:ayrshare_flutter/ayrshare_flutter.dart';
class ShareScreen extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Share Content'),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
try {
final response = await AyrshareFlutter.share(
post: 'Check out this amazing content!',
platforms: ['facebook', 'twitter', 'instagram'],
mediaUrls: ['https://example.com/image.jpg'],
);
print('Share successful: ${response.toString()}');
} catch (e) {
print('Share failed: $e');
}
},
child: Text('Share Now'),
),
),
);
}
}