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
Related
On android I'm using Facebook SDK 4.0. I want to get all photos of a user logged in via Facebook login.
I have this URL
https://graph.facebook.com/FACEBOOK_ID/albums?access_token=TOKEN
How do I parse and generate images to be viewed in a ListView ?
set Bundle for extra parameters like crate a bundle for required parameters ex:
Bundle parameters = new Bundle();
parameters.putString("fields", "id, picture");
Then set it to GraphRequest.
you have to used below code to get all photos of user.
/* make the API call */
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/{user-id}/photos",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
}
}
).executeAsync();
I am new to android development. I am trying to post photo from an android app to user's facebook album. I added following code after I logged in the user with permissions "user_friends"
new GraphRequest(
MainFragment.mAccessToken.getCurrentAccessToken(),
"/me/photos",
params,
HttpMethod.POST,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
}
}
// This is my code for user login at app start
post = (Button)view.findViewById(R.id.button1);
mLoginManager = LoginManager.getInstance();
mLoginManager.registerCallback(mCallbackManager, mCallback);
mLoginManager.logInWithReadPermissions(this, Arrays.asList("user_friends")); //user_friends // publish_actions
Please guide me
I did a mistake .. Firstly, my question was incomplete .. I have found the issue .. Following seems to be working.
MainFragment.mLoginManager.logInWithPublishPermissions(this, Arrays.asList("publish_actions"));
Bundle params = new Bundle();
params.putString("url", "http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg");
/* make the API call /
new GraphRequest(
MainFragment.mAccessToken.getCurrentAccessToken(),
"/me/photos",
params,
HttpMethod.POST,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/ handle the result */
Log.d("MainFragment:", "onCompleted mAccessToken = " + MainFragment.mAccessToken.getCurrentAccessToken());
}
}
).executeAsync();
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();
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
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();