I am working on proof-of-concept Android app using Graph API. I am trying to upload video to album of my Facebook app.
I use this part of code to do so:
String dataName = new File(getRealPathFromURI(this,data.getData())).getName();
Toast.makeText(this,dataName,Toast.LENGTH_LONG).show();
params.putString("filename", new File(getRealPathFromURI(this,data.getData())).getName());
params.putByteArray("source",k);
/* make the API call */
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/"+sProfile.getId()+"/videos",
params,
HttpMethod.POST,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
TextView textView = (TextView) findViewById(R.id.textView2);
textView.setText(response.toString()+" "+k.length);
Log.v("Response:",response.toString());
}
}
).executeAsync();
Even though, this is Android equivalent to suggested method by Facebook and that I'm trying to upload working .mp4
file, I get this error.
{Response: responseCode: 400, graphObject: null, error: {HttpStatus: 400,errorCode: 352, errorType: OAuthException, errorMessage: Sorry, the video file you selected is in a format that we don't support.}}
It seems to me, there is a bug in GraphRequest.java in Android Facebook SDK, which causes filename of video to be uploaded to be set to just "source".
Here, on line 2166 and 2167 in GraphRequest.java we see:
public void writeBytes(String key, byte[] bytes) throws IOException {
writeContentDisposition(key, key, "content/unknown");
And arguments of writeContentDisposition in the same file on lines 2245-2248:
public void writeContentDisposition(
String name,
String filename,
String contentType
)
Which should, in case of params.putByteArray("source",k);
set filename to "source", with no file extention for facebook to work with.
Adding params.putString("filename", <FILENAME>);
simply doesn't work, because that doesn't change filename wrongly assigned to source
.
In Graph API 2.5 video reference we can read the following:
When including a source parameter, you should include a filename with the correct extension that indicates the type of file container.
Funnily enough, in 2.6 video reference there is nothing like that written there.
My questions are:
.
So, it seems there really is a bug in Facebook Android SDK.
As a workaround, I used Android Asynchronous Http Client, with which I created following method:
void uploadVideo(String name, InputStream is, String fileName){
AsyncHttpClient client = new AsyncHttpClient();
RequestParams rp = new RequestParams();
rp.put("access_token",AccessToken.getCurrentAccessToken().getToken());
rp.put("name",name);
rp.put("source",is,fileName);
client.post(this, "https://graph-video.facebook.com/me/videos", rp, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
//something...
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//something...
}
});
}
And everything works as intended.