I pretend to integrate my website with facebook, whant to make a automatic post (on a specific facebook account) while a user interacts with a web application.
is there any way to make this operation like a webservice way?, authenticating and calling a url that posts the information i send directly on the facebook wall?
i'm using asp.net mvc3 C# i've found a facebook developer toolkit library, is this the correct way to start or what should i do?
What is necessary is only write a post automatically on a facebook account, for example when i write a new article (news) on my website, it will be automatically posted on fb.
any idea to get me started?
I did something kind of similar, when a user clicks on a "share" button on my mvc app, it posts something on his wall. The problem using the oauth dialog, is that it will redirect the browser to a facebook site for the user to log in and accept the application permissions.
On the "share" button, I linked it to this url:
<a href=""https://www.facebook.com/dialog/oauth?client_id=[YOUR_APP_ID]&redirect_uri=[THE_REDIRECT_PAGE]/&scope=publish_stream"">
<img src='@Url.Content("~/Images/facebook_share.png")' alt="Share on Facebook!" style="height:28px" />
</a>
YOUR_APP_ID is your facebook application ID.
THE_REDIRECT_PAGE is a public page on your site that facebook will automatically redirect once the user has logged in and accepted the permissions. When facebook redirects, it appends a querystring parameter called "code" to it.
NOTE: The redirect page MUST END with a "/", it cannot end with a document, or else it doesn't work!
Once the user has accepted your request, you must ask facebook another code, called access code, used to post on the user's wall.
This code is on the redirect page:
public ActionResult Index(string code)
{
string fbAuthCode = Request["code"]; //The authorization code.
string fbAppId = "XXXXXXX"; //Your fb application id.
string fbSecretAppId = "XXXXXXXXXXXXXXXXXXXXX"; //Your fb secret app id, it is found on the fb application configuration page.
string redirectUrl = string.Format("[THE_REDIRECT_PAGE]", locationPointId, entryLocationId); //The redirect url. THIS MUST BE THE EXACT SAME REDIRECT URL USED ON THE JAVASCRIPT LINK!
string fbUrl = "https://graph.facebook.com/oauth/access_token?client_id=" + fbAppId + "&redirect_uri=" + redirectUrl + "&client_secret=" + fbSecretAppId + "&code=" + fbAuthCode; //Url used to post.
string accessToken = string.Empty;
try
{
WebClient client = new WebClient();
using (Stream stream = client.OpenRead(fbUrl))
using (StreamReader reader = new StreamReader(stream))
{
accessToken = reader.ReadToEnd().Split('&')[0].Replace("access_token=", string.Empty);
reader.Close();
}
}
catch (Exception ex)
{
throw new Exception("An error ocurred while trying to get the fb token in " + fbUrl, ex);
}
Once you have the access token, you can post to the user wall:
string postUrl = "https://graph.facebook.com/me/feed";
string postParameters;
postParameters = string.Format("message={0}&picture={1}&name={2}&caption={2}&description={3}&link={4}&access_token={5}",
"[Message]",
"[PictureUrl]",
"[Name]",
"[Caption]",
"[Link]",
accessToken);
try
{
System.Net.WebRequest req = System.Net.WebRequest.Create(postUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(postParameters);
req.ContentLength = bytes.Length;
using (System.IO.Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length); //Push it out there
os.Close();
using (WebResponse resp = req.GetResponse())
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
ViewBag.PostResult = sr.ReadToEnd().Trim();
sr.Close();
}
os.Close();
}
}
catch (Exception ex)
{
throw new Exception("An error ocurred while posting data to the user's wall: " + postUrl + "?" + postParameters, ex);
}
return RedirectToAction(XXXXXXXXXXXxx....); //Then i redirect to another page.
You can see that on the exception I throw the posted url (for debugging purposes).
With that url you can usually go to the facebook Graph API Explorer or the Linter and check the real error.
I don't know if this is exactly what you want but hope it gives you a kickoff...
I've struggled a few days with this, because facebook documentation on open graph is not very good yet, at least for us that don't use curl :)
https://developers.facebook.com/docs/opengraph/tutorial/
https://developers.facebook.com/docs/opengraph/
Hope it helps.
MT.
It's easy:
Step 1:
Get a valid facebook token by requesting "*https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope="publish_stream,email*" (full list of permissions there: https://developers.facebook.com/docs/reference/api/user/)
Step 2:
Post your message to the user wall:
curl -F 'access_token=...' \
-F 'message=your message' \
https://graph.facebook.com/ID_OR_USERNAME/feed