Search code examples
androidfacebook-graph-apifacebook-android-sdkfacebook-access-tokenfacebook-authentication

Facebook Android SDK - can read profile, but not publish photo to wall


I am using this library for intergrating facebook to my app: https://github.com/sromku/android-simple-facebook . It works just fine when accessing profile, friends list, etc. However, when I am trying to publish a photo on the wall, I use the regular SDK, but it does not work. In this case I don't use the library because it does not support publishing photos to wall/pages/groups.

I pass the session I have after requesting publish permissions and fire this request:

public void uploadToFaceWall() {
    Session session = mFaceSession;
    if (session != null){
        String fbPhotoAddress = null;
        Request.Callback uploadPhotoRequestCallback = new Request.Callback() {
            @Override
               public void onCompleted(Response response) {
                if (response.getError() != null) {
                    Log.d(TAG, "error: "+response.toString()):
                } 

                Object graphResponse = response.getGraphObject().getProperty("id");
                if (graphResponse == null || !(graphResponse instanceof String) || 
                    TextUtils.isEmpty((String) graphResponse)) {
                    Log.d(TAG, "failed photo upload/no response");
                } else {
                    finish();
                } 
             }
          };
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeFile(myFacePost.getImages().getString(0), options);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Bundle parameters = new Bundle();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        parameters.putByteArray("picture", byteArray);
        parameters.putString("message", "Check out my picture");
        Request req = new Request(session, "me/photos", parameters, HttpMethod.POST, uploadPhotoRequestCallback);
        req.executeAndWait();
    }
}

However, this does not work, and the response contains an error:

{Response:  responseCode: 200, graphObject: null, error: {HttpStatus: -1, errorCode: -1, errorType: null, errorMessage: java.lang.NullPointerException}, isFromCache:false}

mFaceSession is the session I have after the user has accepted the new publish permissions.

When debugging I see that the session is OPENED.

Any idea why this does not work?


Solution

  • I've tested your code with the HelloFacebook sample that's shipped with the SDK, replaced here: https://github.com/facebook/facebook-android-sdk/blob/master/samples/HelloFacebookSample/src/com/facebook/samples/hellofacebook/HelloFacebookSampleActivity.java#L351

    And it works fine. The possible issue you're facing is the last line req.executeAndWait(); You'll face an exception if you're doing this on the main thread, so update that to req.executeAsync() as in the sample and it should work.

    Also see that you get a 200 code which is success, you can see if it worked or not by going to that users timeline. The NullPointerException is probably from this line: Object graphResponse = response.getGraphObject().getProperty("id");.