I am working on a small module which needs to automatically post links on a Facebook page.
I have done the following setup:
- Created a Facebook page (the one who will display the links)
- Created a Facebook app
- Added the app both on the page administrator profile as well as on the Facebook page with the following permissions: publish_stream, manage_pages
In order to obtain a valid access token (manually for now - I know it is not recommended, but it does the job in my case) I have done the following:
- Logged in as the page administrator
- Navigate to the page: https: / / developers.facebook.com/tools/access_token/
- Copy the user token and make it non-transient by GET-ting the following url: https: / / graph.facebook.com/oauth/access_token?client_id={app id}&client_secret={app secret}&grant_type=fb_exchange_token&fb_exchange_token={the token which I copied earlier}
- Get the page access token from the user token resulted from step 3 by firing a GET to: https://graph.facebook.com/me/accounts?access_token={the never expire access token}
From the json which I get after step 4 I copy the access token of the page which I am interested in and post the link with the following C# code:
try
{
dynamic data = new ExpandoObject();
var fbm = message as FacebookMessage;
data.access_token = ConnectionData.AuthenticationToken;
data.message = fbm.Message;
data.link = fbm.Link;
data.name = fbm.Name;
data.caption = fbm.Caption;
data.description = fbm.Description;
if (!String.IsNullOrEmpty(fbm.Icon))
{
data.picture = fbm.Icon;
}
var @params = ((ExpandoObject)data).ToUrl();
var _currentRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(String.Format("https://graph.facebook.com/{0}/feed?{1}", _currentEndpoint.ObjectID, @params));
_currentRequest.Method = "POST";
_currentRequest.ContentType = "application/json; charset=utf-8";
_currentRequest.UserAgent = "Mozilla/4.0 (compatible; Windows NT)";
_currentRequest.Accept = "*/*";
_currentRequest.CookieContainer = new CookieContainer();
_currentRequest.KeepAlive = false;
//_currentRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
//using (var sw = new StreamWriter(_currentRequest.GetRequestStream(), Encoding.UTF8))
//{
// sw.Write(json);
//}
string json;
var resp = _currentRequest.GetResponse();
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
json = sr.ReadToEnd();
}
var jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var output = jsonSerializer.Deserialize<dynamic>(json);
return new OperationStatus();
}
catch (Exception ex)
{
return new OperationStatus { Exception = ex };
}
}
The "_currentEndpoint.ObjectID" is the Object ID of the page.
The POST is done successful, and I see the link being posted both on my administrator's wall as well as on the Page itself as long as I am logged in as admin.
The sooner I log out, I can't see any posted links nor anyone else who liked the page.
What gives? How can I make the links to show up on the page wall and also on the walls of the people who liked the page.
I did check spam on the page, and I don't see my links there, and from the privacy settings everything seems visible.