如何让 Flutter 应用程序点赞 Facebook。我已经编写了此代码,使用 Flutter 和 Firebase 在 Facebook 上点赞。但是当我尝试完成它时总是显示此错误。没有错误
如何让 Flutter 应用程序提供 Facebook 的点赞。
我编写了此代码以使用 flutter 和 firebase 在 Facebook 上点赞。
但是当我尝试完成它时总是显示此错误。我的代码没有错误,我认为它是 faceboob-gragh-api 问题
获取点赞时出错:{\'error\':{\'message\':\'不支持获取请求。ID 为“okxaHZh7gPfu3rev”的对象不存在,由于缺少权限而无法加载,或者不支持此操作。请阅读 Graph API 文档:https://developers.facebook.com/docs/graph-api\',\'type\':\'GraphMethodException\',\'code\':100,\'error_subcode\':33,\'fbtrace_id\':\'APZvYG8aMW6bU-fewHOzPPT\'}}
并且这个错误也
错误异常:无法点赞帖子:不支持帖子请求。ID 为“okxaHZh7gPfu3rev”的对象不存在,由于缺少权限而无法加载,或者不支持此操作。请阅读 https://developers.facebook.com/docs/graph-api
这是我使用的代码。
Future<void> likePost() async {
final url = urlController.text;
final postId = extractPostId(url);
if (postId == null) {
print(false);
return;
}
print('Extracted post ID: $postId');
try {
List<UserModel> users = await FireStoreUser().getUsers();
users.shuffle();
int likesGiven = 0;
for (UserModel user in users.take(20)) {
if (user.facebookToken == null || user.facebookToken!.isEmpty) {
print('User ${user.id} has no valid Facebook token.');
continue;
}
bool alreadyLiked =
await checkIfUserLikedPost(user.facebookToken!, postId);
if (!alreadyLiked) {
await giveLike(user.facebookToken!, postId);
likesGiven++;
}
if (likesGiven >= 20) break;
}
print(true);
} catch (e) {
ShowDiloagAlretService().showDiloagAlret(
text: 'Ok',
onPressed: () {
Get.back();
},
text2: '',
onPressed2: () {},
title: 'Error liking the post: $e',
height: 200,
);
print('error $e');
}
}
String? extractPostId(String url) {
final regexList = [
RegExp(r'facebook\.com/.+/posts/(\d+)'),
RegExp(r'facebook\.com/photo\.php\?fbid=(\d+)'),
RegExp(r'facebook\.com/.+/videos/(\d+)'),
RegExp(r'facebook\.com/story\.php\?story_fbid=(\d+)'),
RegExp(r'facebook\.com/.+/permalink/(\d+)'),
RegExp(r'facebook\.com/share/p/(\w+)'),
];
for (final regex in regexList) {
final match = regex.firstMatch(url);
if (match != null && match.groupCount > 0) {
return match.group(1);
}
}
return null;
}
Future<bool> checkIfUserLikedPost(String token, String postId) async {
final response = await http.get(
Uri.parse('https://graph.facebook.com/$postId/likes?access_token=$token'),
);
if (response.statusCode == 200) {
final data = json.decode(response.body);
for (var like in data['data']) {
if (like['id'] == 'me') {
return true;
}
}
} else {
print('Error fetching likes: ${response.body}');
}
return false;
}
Future<void> giveLike(String token, String postId) async {
final response = await http.post(
Uri.parse('https://graph.facebook.com/$postId/likes'),
body: {
'access_token': token,
},
);
if (response.statusCode != 200) {
final errorResponse = json.decode(response.body);
final errorMessage = errorResponse['error']['message'];
print('Failed to like post: $errorMessage');
throw Exception('Failed to like post: $errorMessage');
}
}