User must have accepted TOS - Facebook Graph API error when posting photos to group page - facebook

I've been struggling to upload an image from the user's computer and posted to our group page using the Facebook Graph API. I was able to send a post request to facebook with the image however, I'm getting this error back: ERROR: (#200) User must have accepted TOS. To some extent, I don't believe that I need the user to authorize himself as the photo is being uploaded to our group page. This below, is the code i'm using:
if($albumId != null) {
$args = array(
'message' => $description
);
$args[basename($photoPath)] = '#' . realpath($photoPath);
$ch = curl_init();
$url = 'https://graph.facebook.com/'.$albumId.'/photos?'.$token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
$photoId = json_decode($data, true);
if(isset($photoId['error'])) die('ERROR: '.$photoId['error']['message']);
$temp = explode('.', sprintf('%f', $photoId['id']));
$photoId = $temp[0];
return $photoId;
}
Can somebody tell me if I need to request extra permissions from the user or what i'm doing wrong?
Thanks very much!
Actually, I never succeeded in this :(. As a work around, we created a new facebook user instead of a group page.

This is a known bug and it looks like they're working on it:
http://bugs.developers.facebook.net/show_bug.cgi?id=11254

Related

How to post to Facebook page as page itself

I am developing a blog module. When the admin add a new blog post, a new post will be made in the facebook wall of a page(Let's call it Mypage). The code for it is:
$_accessKey = FB_ACCESS_KEY;
$_feedId = FB_FEED_ID;
$attachment = array(
'access_token' => $_accessKey,
'message' => $postData['message'],
'name' => $postData['name'],
'description' => $postData['caption']
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$_feedId.'/feed');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close ($ch);
In the above code:
FB_ACCESS_KEY is the access token of the App from an user Test
FB_FEED_ID is the feed id of the page.
Now the posting to the Mypage is working, but the posts are coming as Added by Test.
How can I make the post as if it was made by the Mypage itself?
Test is an administrator of the page Mypage.
Authenticating as a Page should solve the issue for you: https://developers.facebook.com/docs/authentication/pages/

facebook, post comment via php curl

From one tutorials here:
Post a reply on a comment in Facebook using cURL PHP / Graph API
I tried to post comment to after one post, But return:
[type] => OAuthException [message] => (#200) The user hasn't authorized the application to perform this action
This is not my app acount wall's post id, I tried to my freinds wall, he have allowed my app, I can post a post to his wall, but failed in a comment, I got the post id via
https://graph.facebook.com/<his fid>/feed?access_token=140XXXXXXXXXXX, so the post id has no problem.
What steps have I missed?
$fbId = '100001102789652_233997699980321';
$accessToken = '140XXXXXXXXXXXX';
$url = "https://graph.facebook.com/{$fbId}/comments";
$attachment = array(
'access_token' => $accessToken,
'message' => "Hi comment",
);
// set the target url
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$comment = curl_exec($ch);
curl_close ($ch);
$comment = json_decode($comment, TRUE);
print_r($comment);
?>
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
should be
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($attachment));
as you can't post an array

Post to Facebook wall with script, with automatic login

I'm able to post to my Facebook wall page when logged in using a PHP script (and the Graph API). I have a news page on my main website, and would like to push news items out to Facebook as they are posted to my site (which I guess is backwards from how many people use the API).
I've granted offline_access and publish_stream to my FB account, for the FB application.
If I close my FB session and try to post from my news page, Facebook presents a login page. I've granted access, so I don't know what else I'm missing.
If I understand your question correctly, I think I have just solved the same problem. I could not solve it entirely using the graph API, but a combination of the graph api and legacy rest api did the trick. Once you have granted permission for your app to post to your facebook page wall, the following code should do what you want:
$url = "https://graph.facebook.com/oauth/access_token";
$client_id = "XXXXXXXXXXXXXX";
$client_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$postString = "client_id=$client_id&client_secret=$client_secret&type=client_cred";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postString);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$token = curl_exec($curl);
curl_close($curl);
$message = rawurlencode($description.'. Notes:'.$notes);
$url = "https://api.facebook.com/method/stream.publish";
$postString = "message=$message&uid=XXXXXXXXXXXXX&$token";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postString);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
Few things worth noting.
1. The uid in the rest api $postString is the uid for your facebook PAGE. You do not need a target_id because when the uid is of a page it can only publish to itself.
2. Your $message must be rawurlencoded because that is the form accepted by the rest api.
3. This will post to your page as your APP, not as the user who made the post to your website. To do that you would need to have the publish_stream permission from all of your users.
Hope this helps.

How do i retrieve the url of an image uploaded via Facebook's graph api

I've successfully uploaded an image to user's album using the following code:
$ch = curl_init();
$url = 'https://graph.facebook.com/'.$uid.'/photos?access_token='.$token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
can anyone tell me how I would then retrieve the public link to that image?
I have more experience FQL however, it looks like this might work:
https://graph.facebook.com/me/picture
Documentation is under "connections"
http://developers.facebook.com/docs/reference/api/user
If you choose to use FQL:
http://developers.facebook.com/docs/reference/fql/
http://developers.facebook.com/docs/reference/fql/profile
Succesful photo upload returns id for the uploaded picture. Use that id to request extended info about the picture (graph.facebook.com/[photoid]?access_token=...)

Add a wall post to a page or application wall as page or application with facebook graph API

I wan't to create a new wall post on a appliaction page or a "normal" page with the facebook graph API. Is there a way to "post as page"? With the old REST-API it worked like this:
$facebook->api_client->stream_publish($message, NULL, $links, $targetPageId, $asPageId);
So, if I passed equal IDs for $targetPageId and $asPageId I was able to post a "real" wall post not caused by my own facebook account.
Thanks!
$result = $facebook->api("/me/accounts");
foreach($result["data"] as $page) {
if($page["id"] == $page_id) {
$page_access_token = $page["access_token"];
break;
}
}
$args = array(
'access_token' => $page_access_token,
'message' => "I'm posting as a Page!"
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
To publish as Page you need to add manage_pages permission first of all (and get the tokens).
Next use something like this:
$url = 'https://api.facebook.com/method/stream.publish?message=TEST&target_id=PAGEID&uid=PAGEID&access_token=YOUR_TOKEN';
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
Set the value of targetpageid=null and check the output...