Uploading photos to Facebook: Adding parameters before executing the newUploadPhotoRequest call gives the 'OAuthException' error

我怕爱的太早我们不能终老 提交于 2019-12-06 05:09:22

问题


Summary: Trying to post an image, and description on Facebook. If the image URL is null, I use a local drawable default image to upload to photos.

To upload the local image, I am using the following code:

My code:

Request.Callback uploadPhotoRequestCallback = new Request.Callback() {
    @Override
    public void onCompleted(Response response) {
        if (response.getError() != null) { 
            //post error
        } else{
            String uploadResponse = (String) response.getGraphObject().getProperty("id");
            if (uploadResponse is valid) { 
                  parameters.putString("message", description);
            } else { 
                  //error
            } 
        }
    }
};

Request request = Request.newUploadPhotoRequest(Session.getActiveSession(), BitmapFactory.decodeResource(_context.getResources(), R.drawable.default_image), uploadPhotoRequestCallback);
        request.setParameters(parameters);
        Request.executeBatchAsync(request);

This executes with the following:

{HttpStatus: 400, errorCode: 324, errorType: OAuthException, errorMessage: (#324) Requires upload file}

Notes:
1. The image uploads successfully without request.setParameters(parameters);
2. Tried parameters.putString("name", description); instead of parameters.putString("message", description);

Why is adding parameters causing an OAuthException?


回答1:


When you create an newUploadPhotoRequest, that method will implicitly add some parameters to the Request object that's returned. When you call setParameter, you're replacing those implicit parameters with your own, which causes the request to be missing some required params.

What you should do instead is:

Request request = Request.newUploadPhotoRequest(Session.getActiveSession(), BitmapFactory.decodeResource(_context.getResources(), R.drawable.default_image), uploadPhotoRequestCallback);
Bundle parameters = request.getParameters(); // <-- THIS IS IMPORTANT
parameters.putString(...);
// add more params here
request.setParameters(parameters);
request.executeAsync();


来源:https://stackoverflow.com/questions/17917929/uploading-photos-to-facebook-adding-parameters-before-executing-the-newuploadph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!