Android post picture to Facebook public page wall - android

I'm currently able to post to a public page wall using:
JSONObject json = new JSONObject();
json.put("message", "I'm on your wall");
Request req = Request.newPostRequest(getSession(), "PowerCardSoftware/feed", GraphObject.Factory.create(json), new Callback() {
#Override
public void onCompleted(Response response) {
if(response.getError() != null)
Log.e("FRAGACTIVITY", response.getError().toString());
Toast.makeText(getBaseContext(), "I hacked your facebook!", Toast.LENGTH_SHORT).show();
}
});
Request.executeBatchAsync(req);
I would like to post a picture the user takes onto the public wall as well. I've tried using a Bundle instead of a JSONObject and using each of these lines:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
postPhoto.compress(CompressFormat.JPEG, 100, baos);
params.putByteArray("picture", baos.toByteArray());
params.putByteArray("source", baos.toByteArray());
They both give me an error like this - errorMessage: (#100) picture URL is not properly formatted
Anyone know how to post a photo onto someone else's facebook wall without using deprecated functions/Objects in the facebook sdk?

This is my code to upload a photo stored locally on the phone:
Request request6 = Request.newUploadPhotoRequest(
session,
((BitmapDrawable) getResources().getDrawable(
R.drawable.picture)).getBitmap(), callback6);
RequestAsyncTask task6 = new RequestAsyncTask(request6);
task6.execute();
This is to upload on your own wall. Reason why there is no option to choose another recipient is due to the breaking changes in February that will disable posting to other people's wall.
See my earlier answer.
EDIT:
what is the best way to upload a photo that will show up on a place's wall with a photo and message?
Can you try this and see if this works?
Bundle parameters = new Bundle();
parameters.putParcelable("picture", YOUR_BITMAP_HERE);
parameters.putString("message", "my message for the page");
return new Request(session, "PowerCardSoftware/feed", parameters, HttpMethod.POST, callback);
Can I add a message using newUploadPhotoRequest()?
No, to add a message with your photo, you won't be using newUploadPhotoRequest. If you dig into the source, its just a wrapper of a Request, so do the same as the method, but add an additional parameter, message, with the message you want, and execute it. I haven't personally verified it but it should work. Let me know if it doesn't.

This is my solution.
I referenced Jesse Chen's answer and made some modifications.
Bitmap image = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "demo.jpg");
Bundle parameters = new Bundle();
parameters.putParcelable("source", image);
parameters.putString("message", "my message for the page");
Request request = new Request(Session.getActiveSession(), "me/photos", parameters, HttpMethod.POST, new Request.Callback() {
#Override
public void onCompleted(Response response) {
showPublishResult(mainActivity.getString(R.string.photo_post), response.getGraphObject(), response.getError());
}
});
request.executeAsync();

You can check facebookSDK project /tests folders and search keywords newPostRequest.
the sample code is as below
GraphObject statusUpdate = GraphObject.Factory.create();
String message = "message";
statusUpdate.setProperty("message", message);
statusUpdate.setProperty("link", "http://stackoverflow.com/questions/14129546/android-post-picture-to-facebook-public-page-wall#");
Request request = Request.newPostRequest(Session.getActiveSession(), "me/feed", statusUpdate, new Callback() {
#Override
public void onCompleted(Response response) {
}
});
request.executeAsync();

Related

Android facebook upload photo on a feed page

Hi guys I am trying to upload a photo to a page on Facebook. This is the page i am trying to post the photo https://www.facebook.com/manegedagen/.
I have been using the following code :
public static void postImageOnWall(Bitmap pBitmap) {
Bundle bundle =new Bundle();
bundle.putString("method", "photos.upload");
bundle.putString("picture","http://www.demos.com/LangGuage/medal_1.png");
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/2091172661108128/feed",
null,
HttpMethod.POST,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
Log.e("", "");
}
}
).executeAsync();
}
But i am getting an error message: OAuthException: errorMessage: Invalid parameters. I would really appreciate if i can get any guide or help.
Try with parameters not as null
see the selected answer from this reference:
https://stackoverflow.com/a/29770773/6011938

Post image from android to facebook page

I have successfully post a feed in facebook page form the graph api.
try {
resObj.put("message","feed from android");
//resObj.put("object_attachment",bitmap);
} catch (JSONException e) {
e.printStackTrace();
}
GraphRequest request = GraphRequest.newPostRequest(
AccessToken.getCurrentAccessToken(),"363453267193844/photos",resObj,
new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
Log.i(TAG,"post page response::"+graphResponse);
}
}
);
request.executeAsync();
But, I'm unable to post image into facebook page. The problem is I'm unable to find the key for image attachment in Json data posted in Graph Api.
The failed response from facebook is
{Response: responseCode: 400, graphObject: null, error: {HttpStatus: 400, errorCode: 324, errorType: OAuthException, errorMessage: (#324) Requires upload file}}
Finally, finally, I was able to post an image into facebook page. This is how I did to post an photo.
Bundle bundle=new Bundle();
bundle.putByteArray("object_attachment",byteArray);// object attachment must be either byteArray or bitmap image
bundle.putString("message","some message here");
GraphRequest graphRequest=new GraphRequest(AccessToken.getCurrentAccessToken(),
"{page_id}/photos",
bundle,
HttpMethod.POST,
new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
Log.i("post page response::" + graphResponse);
}
);
graphRequest.executeAsync();
1.) Make sure you a page access token with publish_pages permission that can be used to publish new photos.
2.) From the docs . Note that you dont have a "/" before pageid in your call.
There are two separate ways of publishing photos to Facebook:
1: Attach the photo as multipart/form-data. The name of the object
doesn't matter, but historically people have used source as the
parameter name for the photo. How this works depends on the SDK you
happen to be using to do the post.
2: Use a photo that is already on the internet by publishing using the
url parameter:
Bundle params = new Bundle();
params.putString("url", "{image-url}");
/* make the API call */
new Request(
session,
"/{page-id}/photos",
params,
HttpMethod.POST,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
}
}
).executeAsync();
There is no way to publish more then one photo in the same graph API
call.
3.) Example ==>
Try it like this i.e post byteArrayStream of your photo
postParams = new Bundle();
postParams.putString("message", "feed from android");
postParams.putBoolean("published",true);
String pageID = "363453267193844";
//Post to the page as the page user
ByteArrayOutputStream stream = new ByteArrayOutputStream();
<YOURBITMAPPHOTOHANDLE>.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
postParams.putByteArray("source", byteArray);
postParams.putString("access_token", "your_page_access_token");
/* make the API call */
new Request(
sessionInstance,
"/" + pageID + "/photos",
postParams,
HttpMethod.POST,
new Request.Callback() {
public void onCompleted(Response response) {
//An error occurred during posting to facebook
FacebookRequestError error = response.getError();
if (error != null) {
isPostingError = true;
postingErrorMessage = error.getErrorUserMessage();
} else {
isPostingError = false;
}
}
}
). executeAsync();

what is {image-data} in facebook android sdk

here is the facebook sample
Bundle params = new Bundle();
params.putString("source", "{image-data}");
/* make the API call */
new Request(
session,
"/me/photos",
params,
HttpMethod.POST,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
}
}
).executeAsync();
what is {image-data}
I tried use byte[].toString, file.toString, path of file. not work.
so how to upload photo with this api?
facebook doc is wrong.
change
params.putString("source", "{image-data}");
to
params.putByteArray("source", "{image-data}");
If you want to post a photo, use one of the Request.newUploadPhotoRequest methods, it will set everything up for you.
See https://developers.facebook.com/docs/reference/android/current/class/Request/#newUploadPhotoRequest

Android: Sharing images via Facebook API

I am trying to share an image via the android facebook API to facebook.
Sharing text and links works fine, uploading an image, too.
I found out, that I only can post images, if they are online or in the photoalbum of the user.
Bundle bundle = new Bundle();
bundle.putParcelable("picture", b);
Request request = new Request(session, "me/photos", bundle,
HttpMethod.POST, new Request.Callback() {
#Override
public void onCompleted(Response response) {
String imageid = (String) response.getGraphObject()
.getProperty("id");
share(session, text, imageid);
}
});
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
imageid is a number like "123456789908765345678"
After the execution, "share" is called:
Bundle bundle = new Bundle();
bundle.putString("caption", "some text");
bundle.putString("description", "Imageid:"+imageid);
bundle.putString("link",
"https://link.de");
bundle.putString("name", "some text");
bundle.putString("place", imageid); //It doesn't work for me
new WebDialog.FeedDialogBuilder(this, session, bundle).build().show();
I don't know what I am doing wrong. I simply want so share this image.
Help please :)
You have to make another request with the id of the resource in order to get the url of the image you are trying to reference in the share dialog.
So you should do something like this:
Callback callback = new Callback() {
#Override
public void onCompleted(Response response) {
if (response.getGraphObject() != null) {
String imageUrl = (String) response.getGraphObject().getProperty("picture");
// call to your sharing method
share(session, text, imageUrl);
}
}
};
Request request = new Request(Session.getActiveSession(), imageid, null, HttpMethod.GET, callback);
request.executeAsync();
You can upload the photo to facebook album using
com.facebook.Request fbRequest = com.facebook.Request.newUploadPhotoRequest(session, image, callback);
fbRequest.executeAsync();

Android Facebook SDK 3.5 request works only with both "Bundle" and on "graphPath" param

It's my fist post here, so please, go easy :)
I'm integrating Facebook SDK in an Android application. After the user logs in, I want to show him the app's last post in facebook, so I'm triYng to get it with(as graph object):
Bundle params = new Bundle();
params.putInt("limit", 1);
Request request = new Request(session, inRequestId + "/posts/?limit=1&access_token=" +
session.getAccessToken().toString(), params, HttpMethod.GET, new Request.Callback() {
#Override
public void onCompleted(Response response) {
if(response != null){
if(response.getError() != null)
Toast.makeText(getActivity(), "Error retrieveng last post", Toast.LENGTH_SHORT).show();
else {
updateFacebookView( response.getGraphObject().getInnerJSONObject() );
}
}
}
});
request.executeAsync();
This works, fine I think, but if I'm triyng to make it without graphPath's limit=1 parameter (inRequestId + "/posts/?access_token=" + session.getAccessToken().toString()), it has no limit, and if I don't put bundle param "limit", (as string or as int tested), it gives me this error:
09-30 17:51:47.094: E/caca(19703): {HttpStatus: 400, errorCode: 190, errorType:
OAuthException, errorMessage: Malformed access token
CAAT76i2gxTgBAKyLzc8SI6y7V1pGJ0fmbLWCtuKdhHIEZAQBA0jYx4YqZB8IgRDJMUlw1XrvZCLJ8kxKdZCRG3LNbrVL8fB34ZBlyvlqadT192MCWMkst1lMSdFwtRVPWiSNfBfi8Gq2RHZCWskrBVTjAwPDKyDMGLSU8sPnXfe0r2tsZBdZCXLCOJGQtE76sJkr7n8SdOU4j1KopkvT0Mux7QBGf7ZBXtCRqDnsZAZCxSNHAZDZD?access_token=CAAT76i2gxTgBAKyLzc8SI6y7V1pGJ0fmbLWCtuKdhHIEZAQBA0jYx4YqZB8IgRDJMUlw1XrvZCLJ8kxKdZCRG3LNbrVL8fB34ZBlyvlqadT192MCWMkst1lMSdFwtRVPWiSNfBfi8Gq2RHZCWskrBVTjAwPDKyDMGLSU8sPnXfe0r2tsZBdZCXLCOJGQtE76sJkr7n8SdOU4j1KopkvT0Mux7QBGf7ZBXtCRqDnsZAZCxSNHAZDZD}
Am I not using this correctly or it's a bug?
Thanks in advance
The second parameter for the constructor is the "path" of the request, but you're also putting request parameters into the path, which messes with the other parameters the Request class may try to put on your behalf. A couple of things to do:
Don't put request parameters in the path parameter, stick them into the "params" bundle.
Don't put the access token (the Request class automatically gets it from the session object).
Try something like this:
Bundle params = new Bundle();
params.putInt("limit", 1);
Request request = new Request(session, inRequestId + "/posts", params, HttpMethod.GET, new Request.Callback() {
...
});
request.executeAsync();

Categories

Resources