Facebook4j library post photo issue - facebook

I am using the facebook4j library for integrating facebook api in java. However, I face issues when I try to post a photo with a place attribute.
Media media = new Media(new File(<image-path-here>));
PhotoUpdate update = new PhotoUpdate(media);
update.message("photo upload test");
update.setPlace("Bangalore");
facebook.postPhoto(update);
But I get the following exception:SEVERE: Servlet.service() for servlet [post] in context with path [/Sample] threw exception [message - An unknown error has occurred.FacebookException{statusCode=500, errorType='OAuthException', errorMessage='An unknown error has occurred.', errorCode=1, errorSubcode=-1, version=2.2.0}
Turns out that without the 'setplace' its works completely fine. I am not sure if I am doing the right way and searched the web to get some samples as well. But could not find one, can anyone please suggest what am I doing wrong here? I can post the photo on facebook without setting the location but I need to set location for image.
Thanks

With my solution, I upload the image to server and upload that file to facebook. you can see the below example.
/**
* NOTE: Post Photo To Wall Facebook
* #param String idToObject
* #param File filePhoto (This is real file from server)
* #return List<String>
*/
public String postToWallPhoto(String idToObject, UploadedFile uploadedFile) {
PhotoUpdate photoUpdate = null;
try {
Media media=new Media(uploadedFile.getFile());
photoUpdate = new PhotoUpdate(media);
photoUpdate.message(postItem.getGroupMessage());
String idPost = facebook.postPhoto(idToObject, photoUpdate);
} catch (FacebookException e) {
e.printStackTrace();
}
return idPost;
}

Can you try this.
PostUpdate post= update = new PostUpdate("#Minh Nguyen")
.picture(new URL("Img_url"));
facebook.postFeed(id,post);

Related

error accessing a file for Facebook upload

i am trying to upload a video to the users Facebook wall i have the FacebookMobile.uploadVideo in place and appears to to be fine but am getting the following error from facebook:
(#353) You must select a video file to upload.
i think the problem is in accessing the video file for some reason.
public function handleUpload(ev:TouchEvent)
{
trace ("posting to facebook"+ accessCamera.filePath);
var videoInfo:Object = new Object;
videoInfo.title = "test upload on FB api",
videoInfo.description = accessCamera.filePath,
videoInfo.fileName = accessCamera.filePath;
videoInfo.video = accessCamera.filePath;
FacebookMobile.uploadVideo('me/videos', onComplete, videoInfo);
}
private function onComplete( result:Object, fail:Object ):void
{
trace("facebook post onComplete called" );
if (result)
{
//result.id is id of post that was just posted
trace ("great");
}
else if (fail)
{
trace("post Failed");
trace('code: '+fail.error.code);
trace('message: '+fail.error.message);
trace('type: '+fail.error.type);
}
}
accessCamera.filepath is the var where the video file is located on the device and looks like this when traced : file:///mnt/sdcard2/DCIM/Camera/VID_20140304_105813.mp4
Am I accessing the file correctly, does it need to be loaded by the app or is a path fine.
any help would be greatly appreciated.
the issue is i was passing a string variable containing the file location into the params object and the FacebookMobile.uploadVideo requires either a fileReferance or byteArray.

facebook4j retrieve different image size from my facebook posts

i am using facebook4j to load home posts facebook.getHome() and i am getting the profile image and the post image as follows:
facebook4j.User fromUser = facebook.getUser(fbHomePost.getFrom().getId(),
new Reading().fields("picture"));
if (fromUser.getPicture() != null)
facebookUserPostDto.setProfileImage(fromUser.getPicture().getURL().toURI().toString());
if (fbHomePost.getPicture() != null)
facebookUserPostDto.setImageLocation(fbHomePost.getPicture().toURI().toString());
all is working well, but the image i am getting from the URLs are small and have low resolution. any idea how to get the "large" images from facebook using facebook4j API? facebook can provide different image sizes API Reference › Graph API › Pictures
thanks
OK first you have to add user_photos to your scope and make sure you are using a valid acces token NOT a Graph API Explorer Token
OAuthService service = new ServiceBuilder()
.provider(FacebookApi.class)
.apiKey(apiKey)
.apiSecret(apiSecret)
.callback("xxxx")
.scope("publish_actions,user_photos ")
.build();
then try this : (tested)
for(Post p : feed){
System.out.println("********************");
String type = p.getType();
System.out.println("type :"+type);
String idPicVid = p.getObjectId();
if(type.matches("photo")){
System.out.println("idPicVid "+idPicVid);
try{
Photo pic = facebook.getPhoto(idPicVid);
System.out.println(pic.toString());
System.out.println("source "+pic.getSource());
System.out.println("picture "+pic.getPicture());
}catch (Exception e) {
e.printStackTrace();
}
}
}
ok i got a reply from the facebook4j google group on how to go this
retrieve different image size from my facebook posts
Sameeh Harfoush
Brate

Trouble posting comments with the LWUIT Facebook API

I'm working on a mobile project and decided to try out the LWUIT framework for development. So far it has been quite interesting although I wish the documentation was a bit better.
I run into a problem trying to post content to facebook using the recently released Facebook API. I'm able to authenticate without issues. However, when I try to post comments to the user's wall, I get a http 404 error.
Has anyone else had this sort of challenge. Below is an excerpt from my code;
protected boolean onShareScreenPost() {
// If the resource file changes the names of components this call will break notifying you that you should fix the code
//boolean val = super.onShareScreenPost();
Form shareForm = Display.getInstance().getCurrent();
final TextField shareField = findShareField(shareForm);
String postText = shareField.getText();
try {
makeFacebookAuthenticationRequest();
FaceBookAccess.getInstance().postOnWall(me.getId(), postText);
} catch (IOException ex) {
ex.printStackTrace();
//Include a dialog saying unable to post or connect to the internet or whatever
}
return true;
}
private void makeFacebookAuthenticationRequest() throws IOException {
FaceBookAccess.getInstance().authenticate("125527160846284", "http://a.b.c/", new String[]{ "publish_stream"});
me = new User();
FaceBookAccess.getInstance().getUser("me", me, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.out.println("returned user");
}
});
}
Seeing this question 24 hours later makes me feel a bit silly.
The answer was quite simple and staring me in the face all along. I needed to wait for the Facebook API to return a User object before making additional calls to the API. Failure to do this resulted in a null reference for my user object and this was being used in the wall post request causing the facebook api to return a http 404.
Hope this helps someone...

How to post image on facebook fan page using C# facebook sdk on codeplex

Currently I'm working on my HTML 5 ASP.Net Application,
Which has requirement of Graffiti Wall, When user draw something on my Wall(means on my HTML 5 Canvas element), and Press Share Button on my Page, at that time the whole picture should need to be post on one of the Facebook Page.
Now my question is that is this thing possible using C# facebook sdk by codeplex ?
if its possible, than how to post image on facebook fan page using this SDK??
Where can I get the good resource the implement this kind of functionality or similar code.
I've check the all examples given by them, there is no any example which post on the facebook fan page.
Or even other library that can implement this kind of functionality.
I've check this library, and see that it has FacebookClient,ExpandoObject, FacebookMediaObject kind of classes, but how to and where to use this classes,where are the description and sample code.
Thanks,
Jigar Shah
you can post to others wall using "{id}/feed"
if you want to post image/video on wall. Try downloading the samples from nuget.
Install-Package Facebook.Sample
Here is how to do using the graph api.
public static string UploadPictureToWall(string id, string accessToken, string filePath)
{
var mediaObject = new FacebookMediaObject
{
FileName = System.IO.Path.GetFileName(filePath),
ContentType = "image/jpeg"
};
mediaObject.SetValue(System.IO.File.ReadAllBytes(filePath));
try
{
var fb = new FacebookClient(accessToken);
var result = (IDictionary<string, object>)fb.Post(id + "/photos", new Dictionary<string, object>
{
{ "source", mediaObject },
{ "message","photo" }
});
var postId = (string)result["id"];
Console.WriteLine("Post Id: {0}", postId);
// Note: This json result is not the orginal json string as returned by Facebook.
Console.WriteLine("Json: {0}", result.ToString());
return postId;
}
catch (FacebookApiException ex)
{
// Note: make sure to handle this exception.
throw;
}
}

BlackBerry - Facebook extended permissions

I've just found a great sample of Facebook Connect on Blackberry by Eki Y. Baskoro,
The following is a short HOWTO on using Facebook Connect on Blackberry. I created a simple Facade encapsulating the Facebook REST API as well as added 'rough' MVC approach for screen navigation. I have tested on JDE 4.5 using 8320 simulator. This is still work in progress and all work is GPLed.
It works great for reading stuff.
NB Don't forget to get Facebook App Key and set it in TestBB class.
But now I want to post something on my wall. So I've add new method to FacebookFacade class using Stream.publish API:
/***
* Publishes message to the stream.
* #param message - message that will appear on the facebook stream
* #param targetId - The ID of the user, Page, group, or event where
* you are publishing the content.
*/
public void streamPublish(String message, String targetId)
{
Hashtable arguments = new Hashtable();
arguments.put("method", "stream.publish");
arguments.put("message", message);
arguments.put("target_id", targetId);
try {
JSONObject result = new JSONObject(
int new JSONTokener(sendRequest(arguments)));
int errorCode = result.getInt("error_code");
if (errorCode != 0) System.out.println("Error Code: "+errorCode);
} catch (Exception e) {
System.out.println(e);
}
}
/***
* Publishes message on current user wall.
* #param message - message that will appear on the facebook stream
*/
public void postOnTheWall(String message)
{
String targetId = String.valueOf(getLoggedInUserId());
streamPublish(message, targetId);
}
This will return Error code 200, "The user hasn't authorized the application to perform this action"
First I thought it's related with Facebook -> Application Settings -> Additional Permissions -> Publish recent activity (one line stories) to my wall but even checked, no difference...
Then I've found this post explains that issue related with extended permissions.
This in turn should be fixed by modifying url a little in LoginScreen class :
public LoginScreen(FacebookFacade facebookFacade) {
this.facebookFacade = facebookFacade;
StringBuffer data = new StringBuffer();
data.append("api_key=" + facebookFacade.getApplicationKey());
data.append("&connect_display=popup");
data.append("&v=1.0");
//revomed
//data.append("&next=http://www.facebook.com/connect/login_success.html");
//added
data.append("&next=http://www.facebook.com/connect/prompt_permissions.php?" +
"api_key="+facebookFacade.getApplicationKey()+"&display=popup&v=1.0"+
"&next=http://www.facebook.com/connect/login_success.html?"+
"xxRESULTTOKENxx&fbconnect=true" +
"&ext_perm=read_stream,publish_stream,offline_access");
data.append("&cancel_url=http://www.facebook.com/connect/login_failure.html");
data.append("&fbconnect=true");
data.append("&return_session=true");
(new FetchThread("http://m.facebook.com/login.php?"
+ data.toString())).start();
}
Unfortunately it's not working. Still Error Code 200 in return to stream.publish request...
Do you have any suggestions how to resolve this?
Thank you!
I have posted the updated API on my website (http://www.baskoro.web.id/facebook-connect-blackberry-HOWTO.html) and this should solve this issue. Please let me know otherwise.
Salam. Cheers!
Eki