Facebook php sdk4 and app - php

I'm developing with Laravel a package to post on facebook. My code responsible for obtaining and storing the access token of the page post a link besides test makes this operation correctly with an app that I have created test fb . The problem is to create another app on facebook to put it into production , this second app facebook created it with the same configuration that does work but when publishing gives me the following error: (# 200 ) The user hasn 't Authorized the application to perform this action .
This is the code snippet I use for testing .
public function getTest(){
//$accessToken = new AccessToken($this->getParam('TOKEN'));
try {
$page_post = (new FacebookRequest($this->session, 'POST', '/'.$this->getParam('PAGE_ID').'/feed', array(
'access_token' => $this->getParam('TOKEN'),
'link' => 'link',
'description' => 'Hola mundo desde laravel',
'picture' => 'link/img.png',
'message' => 'Messge',
) ))->execute()->getGraphObject()->asArray();
// return post_id
print_r( $page_post );
} catch(FacebookSDKException $e) {
var_dump($this->getParam('TOKEN'));
echo $e->getMessage();
// var_dump($e);
exit;
}
As for the authorization of the application is correctly even when you enter to view user applications see 2 ( The works and which not) , both with the same permissions accepted.

Fixed , I lacked a permit : ' publish_pages '
$params = array(
'scope' => 'manage_pages','publish_actions','publish_stream','publish_pages'
);

Related

Facebook - You don't have required permission to access this profile

I'm trying to build a service that let's users create facebook ads with a custom audience based on our database of emails.
Before creating the facebook ad I want to create a preview of the ad. This works just fine when I login in with my own account (admin of facebook app) but fails when logging in as test user.
This is what the user will do:
1. Visit the website of the service.
2. Login using Facebook account with scope: public_profile,email,manage_pages,publish_pages,business_management,ads_management
3. Select facebook page to use
4. Create AdCreative. From this an ad preview can be made. But it fails creating an Adcreative and gives me the following error:
"error":{"message":"Application does not have permission for this action","type":"OAuthException","code":10,"error_subcode":1341012,"is_transient":false,"error_user_title":"No permission to access this profile","error_user_msg":"You don't have required permission to access this profile","fbtrace_id":"EgTeMOXPCUp"}}
The access token as well as ad account belongs to the facebook app. I tried to use the page access token as well but then I don't have permission to access the ad account.
This is code:
function fbadcreative($url, $message, $carasoul, $fbtoken, $pageid){
$calength = count($carasoul);
$children = array();
for($i = 0; $i < $calength; $i++){
$caitem = $carasoul[$i];
$caitem['hash'] = fbaddimage($caitem['picture'], $caitem['id']);
$child = (new AdCreativeLinkDataChildAttachment())->setData(array(
AdCreativeLinkDataChildAttachmentFields::LINK => $caitem['link'],
AdCreativeLinkDataChildAttachmentFields::NAME => $caitem['name'],
AdCreativeLinkDataChildAttachmentFields::DESCRIPTION => $caitem['description'],
AdCreativeLinkDataChildAttachmentFields::IMAGE_HASH => $caitem['hash'],
));
$children[] = $child;
}
$link_data = new AdCreativeLinkData();
$link_data->setData(array(
AdCreativeLinkDataFields::LINK => $url,
AdCreativeLinkDataFields::CAPTION => $url,
AdCreativeLinkDataFields::MESSAGE => $message,
AdCreativeLinkDataFields::MULTI_SHARE_END_CARD => false,
AdCreativeLinkDataFields::MULTI_SHARE_OPTIMIZED => false,
AdCreativeLinkDataFields::CHILD_ATTACHMENTS => $children,
));
$object_story_spec = new AdCreativeObjectStorySpec();
$object_story_spec->setData(array(
AdCreativeObjectStorySpecFields::PAGE_ID => $pageid,
AdCreativeObjectStorySpecFields::LINK_DATA => $link_data,
));
$creative = new AdCreative(null, 'act_<accountid>');
$creative->setData(array(
AdCreativeFields::NAME => $url,
AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec,
));
try {
$creative->create();
return $creative->id;
//return $creative->read(array(AdCreativeFields::ID,));
} catch (FacebookAds\Http\Exception\AuthorizationException $e) {
echo 'Message: ' . var_dump($e);
$previousException = $e->getPrevious();
// Do some further processing on $previousException
exit;
}
I know this is an older post, but it might be interesting for others to read how to solve this.
You need to give the user that requests via api the Advertise and analyze permissions on the PAGE the ad creative will be created for.
Example request here using the graph explorer:
page_id/assigned_users?user=system_user_id&tasks=['ADVERTISE', 'ANALYZE']
In my case i am getting this same error due to giving the wrong page Id , i was giving the another page id that was not linked to this ad account.

Facebook SDK Status/page

Hello I created an API to post directly, it works very well on my facebook profile, the status is ok when I try to publish a status in my page, the post goes in the visitors publications what do I do ?
I looked in the Facebook Graph API, it would seem that this is a bug .. that you can bypass with using Curl..?
ps/ I edited the information page id, app id, secret app
Thanks in advance for your help
Stéphanie
public function statutPage(){
$fb = new Facebook([
'app_id' => 'my app id',
'app_secret' => 'my app secret',
'default_graph_version' => 'v2.8',
]);
$pageID='my page id,;
$token='A_VALID_USER_ACCESS_TOKEN';
$attachment = [
'access_token' => $token,
'message' => 'Premier message auto',
'name' => 'Première publication sur facebook',
'caption' => 'Legend sous le tire',
'link' => 'https://www.la-programmation.surleweb-france.fr',
'description' => 'Description du lien',
'picture' => 'https://www.google.fr/images/srpr/logo11w.png'
];
try {
$response = $fb->post('/'.$pageID.'/feed/', $attachment);
} catch(FacebookAuthorizationException $e) {
echo 'Graph retourne une erreur: ' . $e->getMessage();
exit;
} catch(FacebookSDKException $e) {
echo 'Facebook SDK retourne une erreur: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
echo 'Posté su Facebook avec l\'id: ' . $graphNode['id'];
}
note apparanlty code below only works for "user profiles" and not for "pages". If you want info on how to handle pages, please look into the answer given at Facebook SDK v5 Post as Page on Wall!
But with that being said, Reading the Facebook PHP Developers Docs for publishing to a feed on Graph API 5.0, I see they're use the FacebookRequest object and then executing the method execute on it. It however returns a GraphObject - which seems deprecated in versions higher than 4.
They also mention to be sure to have publish_actions permissions on the account you're logged in with to auto post.
Reference PHP SDK code from facebook /feed/ SDK docs (in the url scroll down to 'publishing') for Graph API 5.0 - note that it returns a GraphObject - which seems deprecated in versions higher than 4. - I edited the example to use getGraphNode as referred to in https://developers.facebook.com/docs/php/FacebookRequest/5.0.0.
/* PHP SDK v5.0.0 */
/* make the API call */
$request = new FacebookRequest(
$session,
'POST',
'/me/feed',
array (
'message' => 'This is a test message',
)
);
$response = $request->execute();
$graphNode = $response->getGraphNode();
/* handle the result */
Alternatively they also mention using the graph api directly by link to Publish with Graph API.

Facebook Batch Request recognizing access token as user for page admin

I am developing an app to post multiple post messages in one time. Say I have 5 fan pages and 10 groups. It will post them all. I have used graph api method and its working well when using for loop and executing one at a time. But I came to know about facebook batch request. Now the problem is when I to fan pages using fan page access token from (https://graph.facebook.com/100000598120816/accounts) its working fine
$param = array( 'message' => "Demo test " , 'access_token' => "<fan page access token>");
try {
$posted = $facebook->api('/425355934226513/feed/', 'post', $param);
if (strlen($posted["id"]) > 0 ) $success = TRUE;
} catch (FacebookApiException $e) {
$errMsg = $e->getMessage();
$error = TRUE;
}
But when I try to do the same using batch request, It post as the USER, not as PAGE ADMIN.
$arr1[] = array( "method"=>"POST", 'relative_url' => '<fan page id>/feed',"body" => "message=Apps testing..Please ignore this message for page." , 'access_token' => "<FAN PAGE TOKEN>");
$arr1[] = array( "method"=>"POST", 'relative_url' => '<my group id>/feed',"body" => "message=Apps testing..Please ignore this message for page." , 'access_token' => "<USER PAGE TOKEN>");
try {
$posted = $facebook->api("/?batch=".urlencode(json_encode($arr1)), 'post');
$success = TRUE;
} catch (FacebookApiException $e) {
$errMsg = $e->getMessage();
$error = TRUE;
}
P.S Both the above code runs successfully. But in fan page its showing as the USER not PAGE ADMIN.
Thanks in Advance.
I'm pretty sure that only Users can post to groups, not Pages. It works that way even manually posting to groups.
You should try specifying a "fallback" Access Token as described in https://developers.facebook.com/docs/graph-api/making-multiple-requests/#differentaccesstokens This Access Token should be an App Access Token.
Also, I'm not sure if you're not overwriting your $arr1[] variable...
try 'relative_url' => '/feed?access_token=' and the same for the group/user token.
There is a bug that doesn't look for the access_token in the json, even though the documentation states it is allowed.

Post on Facebook without Share Prompt in PHP

Is there a possibility where I can make a button on which when I click, the contents directly get shared on facebook without showing our user the share prompt dialog box?
I referred it online and found that its possible through mobile devices:
http://www.mindfiresolutions.com/How-to-post-message-on-your-facebook-wall-without-using-Facebook-dialog-Box-1419.php
The question is can we make some sort of ajax call and get this done on web apps.
We used Following code
<?php
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'My app ID', /*Your APP ID*/
'secret' => 'My Secret ID', /*Your APP Secret Key*/
'allowSignedRequest' => false
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
if($user_id) {
try {
$user_profile = $facebook->api('/me','GET');
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, print a link for the user to login
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
$response = $facebook->api(
"/me/feed",
"POST",
array (
'message' => 'This is a test message',
'link' => 'www.google.com'
/* 'picture' => '{picture}',
'caption' => '{caption}',
'description' => '{description}'*/
)
);
?>
But its returns : " Fatal error: Uncaught OAuthException: (#200) The user hasn't authorized the application to perform this action throw in file"
Any help would be highly appreciated.
Thanks in advance.
Of course you can using the Graph API. On the button click you just have to make a \POST call to /me/feed.
Learn more about publishing a feed via Graph API and what all parameters are available here.
Permission required: publish_stream
Using PHP SDK:
$response = $facebook->api(
"/me/feed",
"POST",
array (
'message' => 'This is a test message',
'link' => '{link}',
'picture' => '{picture}',
'caption' => '{caption}',
'description' => '{description}'
)
);
Direct HTTP Request-
POST /me/feed
Host: graph.facebook.com
message=This+is+a+test+message
...
You can check your calls in Graph API Explorer
Edit:
To ask for the required permissions:
$params = array(
'scope' => 'publish_stream'
);
$login_url = $facebook->getLoginUrl($params);
You can read more about permissions here.
Try this way: https://developers.facebook.com/docs/reference/php/
When coding the wep app, you only have to provide the App Id and the App Secret, then you should have to specify the content to be posted.
You should try also the Javascript Facebook SDK, you'll find it here: https://developers.facebook.com/docs/javascript or you could go further and try the C# SDK, this one is a little bit more complex, but you will be able to do so much more.
Have fun coding!!!
Just go to https://developers.facebook.com/docs/opengraph/
Review it and place what kind of share you want.
This is the long process to be handle so. you need to study it.

Posting feed after facebook connect with PHP

I'm trying to publish a feed to my users' Facebook wall. I use Facebook Connect for my site. I have generated the link with getLoginUrl() with email, offline_access and publish_stream permissions.
My users' click on this link, authenticate with facebook and comes back to my site. When they do, i'm getting their's ID with getUser().
Then i'm trying to publish a post to their walls. I use this:
$user = $facebook->getUser();
try {
$publishStream = $facebook->api("/$user/feed", 'post', array(
'message' => "Mesaj icerigi",
'link' => 'http://ithinkdiff.net',
'picture' => 'http://thinkdiff.net/ithinkdiff.png',
'name' => 'iOS Apps & Games',
'description'=> 'Checkout iOS apps and games from iThinkdiff.net. I found some of them are just awesome!'
)
);
} catch (FacebookApiException $e) {
print_r($e);
}
But everytime i try to that, i got an error. Api exception or something. What i'm doing wrong?

Categories