I'm developing a hybrid app with angular and ionic for iOS and Android and I have come upon an error when posting with an image to facebook. The error only appears when I run the app on an Android device. I am using the cordova-plugin-facebook4 and queries like get friends and login are working correctly.
The code for that specific part:
function makeCallToFacebookWithImage(url){
console.log("share that to facebook with image");
var facebookString = "/me/photos?method=post&url="+encodeURI(url)+"&caption="+$scope.comment;
console.log("facebookString with image: "+facebookString);
facebookConnectPlugin.getLoginStatus(function(loginStatus){
// if already loggedIn
if(loginStatus.status == "connected"){
facebookConnectPlugin.api(facebookString, ["publish_actions"], function(response){
// success
console.log("success "+JSON.stringify(response));
}, function(response){
// error
console.log("error "+JSON.stringify(response));
});
}
}, function(error){
console.log(error);
})
}
The console.log of line 4:
facebookString with image: /me/photos?method=post&url=https://firebasestorage.googleapis.com/URL?alt=media&token=4420080a-41d8-4816-bf62-5bfb159d1da5&caption=FBTest
Error message:
error {"errorCode":"100","errorType":"OAuthException","errorMessage":"Invalid parameter","errorUserMessage":"Your photos couldn't be uploaded. Photos should be less than 4 MB and saved as JPG, PNG, GIF or TIFF files.","errorUserTitle":"Can't Read Files"}
As you can see I store the image in a firebase database (I replaced the exact url with URL to not publish it here), the image is already uploaded and compressed before I call the Facebook post function and it is in .jpg format, so the error message of the Facebook post is not accurate or does not help me. Besides that, the exact code works on iOS perfectly fine.
I am searching for a solution to this error for a few hours now, so may someone here can help me out with this issue. Did someone had this issue before and may know a solution?
EDIT:
The console.log with complete URL's:
iOS:
/me/photos?method=post&url=https://firebasestorage.googleapis.com/v0/b/appname-50cd0.appspot.com/o/post%252F-KbyHksSP_EV2MTjvCb5.jpg?alt=media&token=64f43c60-47fa-40b2-b85b-e1e5ae1dea13&caption=Rest
Android:
/me/photos?method=post&url=https://firebasestorage.googleapis.com/v0/b/appname-50cd0.appspot.com/o/post%252F-KbxypBFRqs6eyYNk8ZR.jpg?alt=media&token=4420080a-41d8-4816-bf62-5bfb159d1da5&caption=FB
Obviously different pictures, but the URL structures are the same.
EDIT2:
What I tried so far:
- using the URL of an existing picture in the firebase storage, which was taken by the iOS device -> same error.
- added the Android hash keys to the facebook development enviroment -> same error
whitelisted graph.facebook.com in the config.xml with:
<access origin="*graph.facebook.com*" subdomains="true"/>
-> same error
- using encodeURIComponent(url) instead of encodeURI(url) -> same error
I am using a Nexus 5 with Android 6.0.1 as test device, at the moment I don't have the access to another Android device, but I'll try it on a different device as soon as I get one.
EDIT 3:
So I tested it on 2 other Android devices and it did result in the same error
I updated the facebook4 plugin to newest version which support facebook sdk 4.22.1, but the error remains
I tried to change the version in cordova-plugin-facebook4/www/facebook-browser.js in the FB.init function (line 147) from version 2.7 to 2.9 (which is the newest) but the error remains
EDIT 4 SOLVED
finally, the solution is in the comments. It was a bug of cordova-plugin-facebook4 on android devices when you use a download url that contains parameter characters like ? & and =, which firebase storage does.
facebookStringcontains the exact same thing on both Android and iOS? - CBroeencodeURIis the wrong method to use here - try usingencodeURIComponentinstead, an see if that gives different results. (They encode a different set of characters - notablyencodeURIdoes not include?and&, and those need to be encoded most of all.) - CBroe