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
Related
Can anyone help me to get public photos from a Facebook page. I'm developing a mobile app using Xamarin.Forms in which I need to get public photos from Facebook.
Currently I'm using Xamarin.Social to integrate Facebook in the app and I'm able to login into a Facebook account but not able to get public photos.
Use the Xamarin.Auth to query the relative facebook API link. For Example, to get the user name and ID you can use:
var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/v2.4/me"), null, _currentUser);
await request.GetResponseAsync().ContinueWith(t =>
{
if (t.IsFaulted)
return;
else
{
string mail = t.Result.GetResponseText();
Dictionary values =
JsonConvert.DeserializeObject>(mail);
bool s = values.TryGetValue("name", out _name);
bool i = values.TryGetValue("id", out _userId);
if(!s){
_name = "Error Getting Username";
}
if(!i){
_userId = "Error Getting Id";
}
}
});
To explore the Facebook API you can follow this link
Hope this helps.
what i am trying to achieve should be simple by theory but hell by implementation, if you think otherwise please help.
so what i am trying to achieve here is that I have my facebook page which I created a developer acount for it, and what i want is to take albums from the page to my website.
I am using the latest stable facebook sdk for .net 6.8.0, and i am assuming that the graph api is on v2.2.
this is the code i am using below to get the access token and access my albums
try
{
var fb = new FacebookClient();
dynamic result = fb.Get("oauth/access_token", new
{
client_id = "---my application number---",
client_secret = "---my application secret---",
grant_type = "client_credentials"
});
var fb2 = new FacebookClient(result.access_token);
dynamic albums = fb2.Get("my application number/albums");
foreach (dynamic albumInfo in albums)
{
try
{
dynamic albumsPhotos = fb2.GetTaskAsync(albumInfo.id + "/photos");
}
catch (Exception Exception)
{
throw Exception;
}
}
}
catch (Exception e)
{
throw e;
}
The data returned in the dynamic albums variables is empty, the thing that confused me more is when i use facebook graph api explorer the call work fine and it return the albums. so what exactly i am missing.
I also read in some threads that you can access your albums without the secret password if they are public but that didn't work for me either i tried it in javascript but no joy.
Thank you in advance.
Since this is your page, it should be pretty easy to retrieve your own photos using Windows PowerShell and http://facebookpsmodule.codeplex.com Get-FBAlbums.
I have my android application allowing user to share interesting fragments of some texts on the Facebook.
Sharing on the user's wall works fine and is implemented using this tutorial:
http://www.integratingstuff.com/2010/10/14/integrating-facebook-into-an-android-application/
I also have a facebook fan page of my application and I would like to consolidate all such individual shares on that page.
So that when some user shares text on their wall the program would also publish this on facebook fan page, so that if someone is interested in the discussion they could like the fan page and subscribe to all the comments other users make.
My problem is that I can either publish on user's wall or publish on fan page.
How can I do both at the same time?
public void postToWall(){
Bundle parameters = new Bundle();
parameters.putString("message", this.messageToPost);
parameters.putString("description", this.messageDesc);
parameters.putString("link", this.messageLink);
// parameters.putString("target_id", FAN_PAGE_ID);
try {
facebook.request("me");
// String response = facebook.request("me/feed", parameters, "POST");
String response = facebook.request(FAN_PAGE_ID+"/feed", parameters, "POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("") ||
response.equals("false")) {
showToast("Blank response.");
}
else {
showToast("Message posted to your facebook wall!");
}
finish();
} catch (Exception e) {
showToast("Failed to post to wall!");
e.printStackTrace();
finish();
}
}
This line publishes to user's wall
String response = facebook.request("me/feed", parameters, "POST");
And this one to fans page
String response = facebook.request(FAN_PAGE_ID+"/feed", parameters, "POST");
I have set up permissions for my app to publish on the fan page using this post
Simple example to post to a Facebook fan page via PHP?
I had the same issue and I just did the requests via two asyncfacebookrunner classes. So basically they are happening in parallel.
private AsyncFacebookRunner mAsyncFbRunner;
private AsyncFacebookRunner mAsyncFbRunner2;
public void postToWall() {
boolean success = true;
Bundle params = new Bundle();
//this is for posting on the walls
parameters.putString("message", this.messageToPost);
parameters.putString("description", this.messageDesc);
parameters.putString("link", this.messageLink);
mAsyncFbRunner.request("me/feed", params,"POST", new WallPostListener(), success);
mAsyncFbRunner2.request(FAN_PAGE_ID+/"feed", params,"POST", new WallPostListener(), success);
}
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;
}
}
I need to know if it's possible to make it so authorized users can upload photos to a fan page of a company (to the wall) using the graph API.
Also, is it possible to become like (become a fan) of a company page through the api once the user is authorized.
Yes you can. You need to obtain your admin access token by using the Graph API explorer (https://developers.facebook.com/tools/explorer) and with a call to
https://graph.facebook.com/{adminfacebookid}/accounts
this will list all pages and apps your admin has access to. Look for the fan page in question and copy the accessToken.
Next get the albumid by clicking on the id of the page, then adding /albums to the request
armed with this you can then post the image data to the url, using the facebook web client
like this
protected void PublishToPublicGallery(string accessToken, string filename, long albumId, string imagename)
{
var facebookClient = new FacebookClient(accessToken);
var mediaObject = new FacebookMediaObject
{
FileName = filename,
ContentType = "image/jpeg"
};
var fileBytes = System.IO.File.ReadAllBytes(filename);
mediaObject.SetValue(fileBytes);
IDictionary<string, object> upload = new Dictionary<string, object>();
upload.Add("name", imagename);
upload.Add("source", mediaObject);
var result = facebookClient.Post("/" + albumId + "/photos", upload) as JsonObject;
}