In my application, there is a case where I want to retrieve the content (message) of a particular Facebook post. I am able to get the postid id for that particular post, but I am not able to get the content associated with that id. I could not find any FQL query to get the post message with post_id or a Graph API URL.
I have this URL to get the all posts, ../100005002784039/posts?fields=id,name,message, but I want only a particular message of the post while passing the post id.
How can I achieve this?
Thanks to sahil, my URL is like this:
https://graph.facebook.com/"+PostId()+"?access_token=Token);
Code
URL fbmsg = new URL("https://graph.facebook.com/"+trace.getFbPostId()+"?access_token="+TOKEN+"");
URLConnection yc = fbmsg.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
String s = "";
while ( (inputLine = in.readLine()) != null)
System.out.println(inputLine);
Log.d(TAG, "getPostId trace getFbPostId " + inputLine);
s = s + inputLine + "n";
Log.d(TAG, "getPostId trace getFbPostId " + s);
in.close();
System.out.println(s);
Simply make the following query to the API-
/POST_ID
Related
I have a problem when trying to post on my own business time line on facebook.
I get an error that says:
"(OAuthException)(#200) The user hasn't authorized the application to perform this action"
I think this has to do with the scope "publish_actions" in somehow but how can I authorize this publish_actions permission exactly?
I have looked everywhere in my account under "https://developers.facebook.com"
I have put my App for review but I cant add "publish_actions" as a permission for Facebook to review because on this permission there is a message which says: "It looks like you haven't made any API requests to access content with the publish_actions permission in the last 30 days"
But how can I use this permission without having the permission??
I would be very greatful for help if I am on the right track and what I am missing out?
Thank you!
try
{
String appID = "12345";
String appSecret = "123456";
String accessToken = "123|1234_a-b";
String clientToken = "1234567";
String pageID = "123456789";
Facebook.FacebookClient client = new Facebook.FacebookClient(accessToken);
var dicParams = new Dictionary<string, object>();
dicParams["message"] = "Hello on the time line message!";
dicParams["caption"] = string.Empty;
dicParams["description"] = string.Empty;
dicParams["name"] = "Some Name";
dicParams["req_perms"] = "publish_stream,manage_pages,status_update,publish_actions";
dicParams["scope"] = "publish_stream,manage_pages,status_update,publish_actions";
dicParams["access_token"] = accessToken;
var publishResponse = client.Post("/" + pageID + "/feed", dicParams);
MessageBox.Show("You posted on the timeline successfully!");
}
catch (Exception ex)
{ MessageBox.Show(ex.ToString()); }
Attempt 2:
I was looking at the facebook documentation and found an URL which seems relevant to post a message on the business page timeline:
POST graph.facebook.com/{user-id}/feed?message={message}&access_token={access-token}
I have tried to put it down in code like below but again, I receive the same error message:
(OAuthException)(#200) The user hasn't authorized the application to perform this action
Question is what I do wrong. Do I need to get any other access token with "publish_action" permission?. If that is the case I can't figure out how to get such token?
String appID = "12345";
String appSecret = "123456";
String accessToken = "123|1234_a-b";
String clientToken = "1234567";
String pageID = "123456789";
String message = "Hello World";
String requestUrl = "https://graph.facebook.com/" + pageID + "/feed?message=" + message + "&access_token=" + accessToken;
RestSharp.RestClient rc = new RestSharp.RestClient();
RestSharp.RestRequest request = new RestSharp.RestRequest(requestUrl, RestSharp.Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("x-li-format", "json");
request.RequestFormat = RestSharp.DataFormat.Json;
RestSharp.RestResponse restResponse = (RestSharp.RestResponse)rc.Execute(request);
RestSharp.ResponseStatus responseStatus = restResponse.ResponseStatus;
//"(OAuthException)(#200) The user hasn't authorized the application to perform this action"
MessageBox.Show(restResponse.Content.ToString());
I have found a solution which works. This code do post a "Message" to the timeline.
Question This code works for my business page. I also like to post on my personal page for friends. So tried to change the pageID to the the ID which is my personal page: (pageID = "987654321") but still it posts on my business page.
I wonder if I need to get a page token specifically for the personal page as well?
If that is the case, I wonder how this token can be achieved?
/*********************************************************************************************************************************************************************/
//STEPS!!! to get a non-expiring page access token:
//1. access the following URL and note the returned access token within the browser's address bar:
//https://www.facebook.com/dialog/oauth?client_id=APP_ID&redirect_uri=REDIRECT_URI&scope=manage_pages,publish_actions&response_type=token
//2. access the following URL and within the returned data find the desired page's name and note the access token:
//https://graph.facebook.com/me/accounts?access_token=ACCESS_TOKEN_RETURNED_FROM_STEP_1
//3. access the following URL and note the returned access token:
//https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&grant_type=fb_exchange_token&fb_exchange_token=PAGES_ACCESS_TOKEN_FROM_STEP_2
//use the Access Token Debugger to ensure your access token's profile ID matches the desired page's ID and it never expires
String newaccesstoken = "aaaaaaaaaaaabbbbbbbbbbbbbbccccccccccccccddddddddddddddddddd";
/*********************************************************************************************************************************************************************/
String appID = "12345";
String appSecret = "123456";
String accessToken = "123|1234_a-b";
String clientToken = "1234567";
String pageID = "123456789"; //Business page
//String pageID = "987654321"; //Personal page
String redirectURI = "http://example.com";
String message = "Hello Facebook";
String imageURL = "http://example.com/images/example-image.png";
String link = "http://example.co";
String caption = "example";
//Post a message to the time line
String requestUrl = "https://graph.facebook.com/" + pageID + "/feed?message=" + message + "&url=" + imageURL +
"&access_token=" + newaccesstoken;
RestSharp.RestClient rc = new RestSharp.RestClient();
RestSharp.RestRequest request = new RestSharp.RestRequest(requestUrl, RestSharp.Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("x-li-format", "json");
request.RequestFormat = RestSharp.DataFormat.Json;
RestSharp.RestResponse restResponse = (RestSharp.RestResponse)rc.Execute(request);
RestSharp.ResponseStatus responseStatus = restResponse.ResponseStatus;
MessageBox.Show(restResponse.Content.ToString() + "," + responseStatus.ToString());
I am retrieving the complete list of a friend's likes(the list of pages that the user likes) using the code bellow:
Uri ex_a = new System.Uri("https://graph.facebook.com/" + friend_id + "/likes? access_token=" + token);
WebClient WC_a = new WebClient();
WC_a.DownloadStringCompleted += new System.Net.DownloadStringCompletedEventHandler(list_likes);
WC_a.DownloadStringAsync(ex_a);
private void list_likes(object ob, DownloadStringCompletedEventArgs e)
{
JsonObject jo = new JsonObject(e.Result);
JsonArray dataArray = (JsonArray)jo["data"];
if (dataArray.ToString().Length > 2)
{
foreach (JsonObject account in dataArray)
{
list_of_likes.Add(new class_of_likes("http://graph.facebook.com/" + (string)account["id"] + "/picture?type=small", (string)account["name"]));
}
}
}
However, in October 2013, this approach will only retrieve 25 results/request.
I need to know how to create a loop to get the remaining results because facebook uses pagination like:
"paging": {
"next": "https://graph.facebook.com/user_id/likes?limit=25&offset=25&__after_id=last_page_id"
Thank you.
Get pagination section, and parse it to get next_page value, then send a query for it. There's no automatic process or get all method , otherwise spams/bots would be the happiest creature in this world.
I am trying to test the ability to tag friends in messages from our app. It posts to facebook just fine, caption, description, name, picture, and link all display as they should, but in message all it shows is the string in p_message.
string token = Request.Cookies["fbToken"]["value"];
var fbclient = new FacebookClient(token);
var args = new Dictionary<string, object>();
args["message"] = p_message + " #[1550577341:nick.rich.1232]";
args["caption"] = "This is caption!";
args["description"] = "This is description!";
args["name"] = "This is name!";
args["picture"] = "http://nowprovemewrong.com/Images/icon-2.png";
args["link"] = str_redirect_uri;
fbclient.Post("me/feed", args);
formatting #[id:username] any differently results in it appearing in the message, so i'm assuming facebook is recognizing it as something just not a tag
You are not allowed to tag messages like this. The feature #[1550577341:nick.rich.1232] exists but it's for use in Open Graph tagging.
see: https://developers.facebook.com/docs/opengraph/guides/tagging/#mentions
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/
With this code:
string fqlQuery = string.Empty;
string sUIDList = string.Empty;
IDictionary<string, object> Parameters = new Dictionary<string, object>();
FacebookApp fbApp = new FacebookApp();
fqlQuery = "SELECT uid, name, pic, profile_url FROM user WHERE uid IN (" + sUIList + ")";
Parameters.Add("method", "fql.query");
Parameters.Add("query", fqlQuery);
var Info = (JsonArray)fbApp.Api(Parameters);
the method Api in the Facebook SDK uses the RestServer and not the Graph Api.
How can I do a FQL Query with Graph API?
Short answer, you can't. We still need to use fql.query to perform FQL actions.