How to make flutter app give likes of Facebook.
I have write this code to give likes on Facebook using flutter and firebase.
But this error is always show when I try to complete it. there is no wrong with my code I think it with faceboob-gragh-api issue
Error fetching likes: {"error":{"message":"Unsupported get request. Object with ID 'okxaHZh7gPfu3rev' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api","type":"GraphMethodException","code":100,"error_subcode":33,"fbtrace_id":"APZvYG8aMW6bU-fewHOzPPT"}}
and this error also
error Exception: Failed to like post: Unsupported post request. Object with ID 'okxaHZh7gPfu3rev' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api
and this is the code that I use.
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');
}
}