When adding a link post in Facebook, a nice looking description (containing a snippet of text from the linked page) and thumbnail are automatically added to the post.
Is there a way to do this automatically using the Facebook API? I am inclined to think that there is not, because posts added by IFTTT, a popular web application that uses the Facebook API, do not contain descriptions. I am unclear as to whether this is a limitation with the Facebook API, and whether there is any way around it.
Yes, it's possible. You can use the Graph Api Method /profile_id/feed. The method receives the arguments message, picture, link, name, caption, description, source, place and tags. The facebook organize the parameters in a "nice looking summary and thumbnail".
You can get more information in the publishing section in the link http://developers.facebook.com/docs/reference/api/
In c#:
public static bool Share(string oauth_token, string message, string name, string link, string picture)
{
try
{
string url =
"https://graph.facebook.com/me/feed" +
"?access_token=" + oauth_token;
StringBuilder post = new StringBuilder();
post.AppendFormat("message={0}", HttpUtility.UrlEncode(message));
post.AppendFormat("&name={0}", HttpUtility.UrlEncode(name));
post.AppendFormat("&link={0}", HttpUtility.UrlEncode(link));
post.AppendFormat("&picture={0}", HttpUtility.UrlEncode(picture));
string result = Post(url, post.ToString());
}
catch (Exception)
{
return false;
}
return true;
}
private static string Post(string url, string post)
{
WebRequest webRequest = WebRequest.Create(url);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(post);
webRequest.ContentLength = bytes.Length;
Stream stream = webRequest.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
WebResponse webResponse = webRequest.GetResponse();
StreamReader streamReader = new StreamReader(webResponse.GetResponseStream());
return streamReader.ReadToEnd();
}
UPDATE:
Open graph protocol meta tags: http://developers.facebook.com/docs/opengraphprotocol/
Related
In my application image has to post to Facebook,mail.Sorry i am new to windows phone.i don't have a idea.please help me.what i can do first.
If you want to share a status you can use your facebook account on your phone using Share link task (with this method you can only post on your wall) http://msdn.microsoft.com/en-us/library/hh394027%28v=vs.92%29.aspx
To post (pictures, messages or others) on every wall you have access you need some things :
First you need to create a facebook application using this link :
Facebook developers
After that you need to identify yourself using a WebBrowser control with the link related to your application. The application requests authorization to perform certain actions such as posting. You need to detail the authorizations like this :
Dictionary<string, string> uriParams = new Dictionary<string, string>() {
{"client_id", "your app id"},
{"response_type", "token"},
{"scope", "user_about_me, offline_access, publish_stream"}, //The rights
{"redirect_uri", "http://www.facebook.com/connect/login_success.html"},
{"display", "touch"}
};
The Dictionary also contains the redirection uri to define if the operation was successful.
Finally you are authentified and receive an access token. Now you can use a WebRequest POST to post a message using this token :
WebRequest request = HttpWebRequest.Create("https://graph.facebook.com/" + the id of your wall + "/feed");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.BeginGetRequestStream((reqResult) =>
{
using (Stream strm = request.EndGetRequestStream(reqResult))
using (StreamWriter writer = new StreamWriter(strm))
{
writer.Write(client.AccessToken);
writer.Write("&message=" + HttpUtility.UrlEncode(status));
}
request.BeginGetResponse((result) =>
{
try
{
var response = request.EndGetResponse(result);
using (var rstrm = response.GetResponseStream())
{
var serializer = new DataContractJsonSerializer(typeof(FacebookPostResponse));
var postResponse = serializer.ReadObject(rstrm) as FacebookPostResponse;
callback(true, null);
}
}
catch (Exception ex)
{
callback(false, ex);
}
}, null);
}, null);
Here is how to post a message on a facebook page,
You should give the Facebook C# SDK a try
Facebook C# SDK
I have built this in once in an app of mine. They have plenty examples available which should be useful.
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'm trying to upload a photo to a REST api in a Windows Phone 7 application using RestSharp for my Gets/Posts.
The post parameters are as follows:
photo:
The photo, encoded as multipart/form-data
photo_album_id:
Identifier of an existing photo album, which may be an event or group
album
I've created my request, but every time I get back "{\"details\":\"missing photo parameter\",\"problem\":\"The API request is malformed\"}\n
My photo parameter looks like this:
"---------------------------8cd9bfbafb3ca00\r\nContent-Disposition: form-data; name=\"filename\"; filename=\"somefile.jpg\"\r\nContent-Type: image/jpg\r\n\r\n(some binary junk listed here)\r\n-----------------------------8cd9bfbafb3ca00--"
I'm not quite sure if it's a problem with how I'm presenting the binary data for the image (currently in my PhotoTaskCompleted event, I read the contents of e.ChosenPhoto into a byte[] and pass that to a helper method to create the form data) or if I just don't create the form correctly.
I'm just trying to do this a simple as possible, then I can refactor once I know how it all works.
void ImageObtained(object sender, PhotoResult e)
{
var photo = ReadToEnd(e.ChosenPhoto);
var form = PostForm(photo);
var request = new RequestWrapper("photo", Method.POST);
request.AddParameter("photo_album_id", _album.album_id);
request.AddParameter("photo", form);
request.Client.ExecuteAsync<object>(request, (response) =>
{
var s = response.Data;
});
}
private string CreateBoundary()
{
return "---------------------------" + DateTime.Now.Ticks.ToString("x");
}
private string PostForm(byte[] data)
{
string boundary = CreateBoundary();
StringBuilder post = new StringBuilder();
post.Append(boundary);
post.Append("\r\n");
post.Append("Content-Disposition: form-data; name=\"filename\"; filename=\"somefile.jpg\"");
post.Append("\r\n");
post.Append("Content-Type: image/jpg");
post.Append("\r\n\r\n");
post.Append(ConvertBytesToString(data));
post.Append("\r\n");
post.Append("--");
post.Append(boundary);
post.Append("--");
return post.ToString();
}
public static string ConvertBytesToString(byte[] bytes)
{
string output = String.Empty;
MemoryStream stream = new MemoryStream(bytes);
stream.Position = 0;
using (StreamReader reader = new StreamReader(stream))
{
output = reader.ReadToEnd();
}
return output;
}
Hammock for Windows Phone makes this real simple.
You just add the file to the request using the AddFile method and pass it the photo stream.
var request = new RestRequest("photo", WebMethod.Post);
request.AddParameter("photo_album_id", _album.album_id);
request.AddFile("photo", filename, e.ChosenPhoto);
Hum are you sure that your PostForm is correct ? The content-* params should be set in the headers of your POST and not in the body ?
var request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add(HttpRequestHeader.Authorization,"blabla");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
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;
}
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