GuilhE
GuilhE

Reputation: 11901

Publish photo on wall with facebook sdk


I'm trying to publish a photo in my wall using facebook sdk 3.0 for android. Here's the code:

    GraphObject graphObject = GraphObject.Factory.create();

    Bitmap bmp = BitmapFactory.decodeFile(loadFinalImageSavedFullPath());
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();

    graphObject.setProperty("source", Base64.encodeToString(byteArray, Base64.DEFAULT);
    graphObject.setProperty("message", "sup");

    com.facebook.Request.executePostRequestAsync(fSession, "me/photos", graphObject, new Callback() {...}

but I'm always getting this error:

I/facebook(10707): {Response: responseCode: 400, graphObject: null, error: {HttpStatus: 400, errorCode: 324, errorType: OAuthException, errorMessage: (#324) Requires upload file}, isFromCache:false}



I've tried to POST without facebook API:

@Override
        protected Boolean doInBackground(String... params) {
            Bitmap bmp = BitmapFactory.decodeFile(loadFinalImageSavedFullPath());

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();

            List<NameValuePair> pm = new ArrayList<NameValuePair>();
            pm.add(new BasicNameValuePair("access_token", params[0]));
            pm.add(new BasicNameValuePair("message","sup"));
            pm.add(new BasicNameValuePair("source", Base64.encodeToString(byteArray, Base64.DEFAULT)));

            String res = postJSONString(https://graph.facebook.com/me/photos, pm);
            Log.i(TAG, res);
            if (res == null || res.contains("error")) {
                return false;
            }
            return true;
        }

Same problem...

I/facebook(2977): {"error":{"message":"(#324) Requires upload file","type":"OAuthException","code":324}}


Looks like this is the only way I manage it to work:

com.facebook.Request.executeUploadPhotoRequestAsync(fSession, bmp, new Callback() {

Drawback, can't add comment in the POST...

What could it be?
Thanks for your time.

Upvotes: 1

Views: 2266

Answers (1)

GuilhE
GuilhE

Reputation: 11901

Finally got it to work!!!

com.facebook.Request request = com.facebook.Request.newUploadPhotoRequest(Session.getActiveSession(), bmp, new Request.Callback() {...});

Bundle params = request.getParameters();
params.putString("name", "sup");
request.setParameters(params);

Request.executeBatchAsync(request);

Hope it helps you too ;)

Upvotes: 4

Related Questions