Can I upload photos to a wall belonging to a fan (company) page wall using the Facebook Graph API? - facebook

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;
}

Related

I can´t get the friendlist from facebook C# SDK (Windows phone)

i want to get the facebook friend's list from my application, but returns me data: []... empty! :(
the scenario it's this:
C#
const string QueryToGetFbInfo = "me";
const string QueryToGetFbPhoto = "me?fields=picture.width(200).height(200)";
const string QueryToGetFriendList = "me/friends?fields=name,picture.width(100).height(100)";
private void btnFacebook_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
FacebookSessionClient fbSession = new FacebookSessionClient("FB_APP_ID");
fbSession.LoginWithApp("public_profile, user_friends, read_friendlists, email", "custom_state_string");
}
private async void tbnShowfriends_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
FacebookSession session = SessionStorage.Load();
FacebookClient client = new FacebookClient(session.AccessToken);
// Get Facebook FriendList
dynamic friends = await client.GetTaskAsync(QueryToGetFriendList);
//Get Facebook user info
dynamic result = await client.GetTaskAsync(QueryToGetFbInfo);
GraphUser user = new GraphUser(result);
//Get profile picture from facebook
dynamic result1 = await client.GetTaskAsync(QueryToGetFbPhoto);
JsonObject json = result1.picture.data;
var picture = (IDictionary<string, object>)json;
....
}
Facebook App Config has the permissions... forgot some ?.. the new panel confuses me a little, Maybe I forgot something in the application settings...
when i select Api Graph, returns me, a friendlist, fine!
but when I select my application, I do not return anything
What I forgot ?... how can i fix this problem ?
You authed Graph Explorer using API v1.0 which means that that app will get all your friends until 4/30/2014. You app you authed using v2.0 or v2.1 which means that you will only get friends that are also using the app

Retriving Facebook posts from Profile via FB api

I have registered an app in facebook developers program and I can retrieve posts from facebook page using fb api, but I cannot retrieve posts from facebook profile. Should I use some different access token for both page or profile? Is there a different way for retrieving posts from facebook page and profile?
Any help will be appreciated!
You may need the user_status permission.
And you call the posts using this graph query {USER_ID}?fields=statuses
Prerequisite:- You need to have valida FB token for all the request:-
I am answering using c# language.
Step 1:- First you need to get the user's FB id using FB token
I am using Facebook SDK 7.0.6 for querying purpose
here's how to initialize the FB service which we will use in our consecutive calls.
FacebookService facebookService = new FacebookService(facebookToken);
var _facebookClient = new Facebook.FacebookClient(token);
Code snippet
public string GetFacebookID(string facebookToken)
{
dynamic result = _facebookClient.Get("me?fields=id");
if (result.ToString().Contains("id"))
{
return result["id"];
}
return string.Empty;
}
after that you can execute below method to get user's post using FB ID and token
public List<Post> GetPostsForUser(string facebookID)
{
List<Post> posts = new List<Post>();
dynamic result = _facebookClient.Get(facebookID + "/posts"); //Case Sensitive, Posts doesn´t work
if (result.ToString().Contains("data") && result.data.Count > 0)
{
foreach (var item in result.data)
{
posts.Add(new Post
{
ID = item.id,
Story = item.story,
Message = item.message,
Created_Time = Convert.ToDateTime(item.created_time),
Reactions = GetReactions(item.id)
});
}
result = _facebookClient.Get(GetNextURL(result.ToString()));
}
return posts;
}

Delete Wall Post on page as page C# SDK

I want to delete the post as page.
I already have access token of the admin user and access token from the page.
My Code:
var fbClient = new FacebookClient { AccessToken = getPageAccessToken() };
dynamic parameters = new ExpandoObject();
parameters.id = postId;
fbClient.Delete(m_GroupId + "/feed",parameters);
I get the following error:
{"error":
{
"type":"OAuthException",
"message":"Invalid token: \"PAGE_ID\". An ID has already been specified."
}
}
I replaced the page id above with PAGE_ID
use the page access token and pass the post id as the path for Delete method.
var fb = new FacebookClient("pageAccessToken");
fb.Delete(postId);
I don't know the C# SDK, but from the look of it you're setting the ID twice, once with parameters.id and again with m_GroupID

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;
}
}

how to create a facebook event by using facebook api in asp.net

How to create a facebook event by using facebook api in asp.net.
Thanks.
public string CreateEvent(string accessToken)
{
FacebookClient facebookClient = new FacebookClient(accessToken);
Dictionary<string, object> createEventParameters = new Dictionary<string, object>();
createEventParameters.Add("name", "My birthday party )");
createEventParameters.Add("start_time", DateTime.Now.AddDays(2).ToUniversalTime().ToString());
createEventParameters.Add("end_time", DateTime.Now.AddDays(2).AddHours(4).ToUniversalTime().ToString());
createEventParameters.Add("owner", "Balaji Birajdar");
createEventParameters.Add("description", " ( a long description can be used here..)");
//Add the "venue" details
JsonObject venueParameters = new JsonObject();
venueParameters.Add("street", "dggdfgg");
venueParameters.Add("city", "gdfgf");
venueParameters.Add("state", "gfgdfgfg");
venueParameters.Add("zip", "gfdgdfg");
venueParameters.Add("country", "gfdgfg");
venueParameters.Add("latitude", "100.0");
venueParameters.Add("longitude", "100.0");
createEventParameters.Add("venue", venueParameters);
createEventParameters.Add("privacy", "OPEN");
createEventParameters.Add("location", "fhdhdfghgh");
//Add the event logo image
FacebookMediaObject logo = new FacebookMediaObject()
{
ContentType = "image/jpeg",
FileName = #"C:\logo.jpg"
};
logo.SetValue(File.ReadAllBytes(logo.FileName));
createEventParameters["#file.jpg"] = logo;
JsonObject resul = facebookClient.Post("/me/events", createEventParameters) as JsonObject;
return resul["id"].ToString();
}
I am using facebook graph apis with FacebookSdk from codeplex.
I am not able to post the venue with this code due to the open bug in facebook API. Other things work fine. I suggest you implement this venue parameters as well so that the functionality will work as soon as facebook resolves this issue.
Mark this as answer if it works for you.It will also help other people to save time on searching.
You might try this http://developers.facebook.com/docs/api