Can I specify the permission when posting status through Graph API? - facebook

When I post a status through the graph api, I can then retrieve the status through "me/feed" or "me/home". But now the newly posted status can only be retrieved through "me/home".
By checking the status's origin data, I found the posted status has the following privacy:
"privacy": {
"description": "Friends",
"value": "ALL_FRIENDS"
},
When I post message through Facebook WEB, I can choose the privacy, which can be one of PUBLIC, FRIENDS, ONLY ME or CUSTOMER. Can I do the same thing with the Graph API?

Do you mean something like:
$wallpost = $facebook->api('me/feed','post',array(
'name' => 'Headline',
'message' => '',
'privacy' => array(
'value' => 'CUSTOM',
'friends' => 'EVERYONE'
)....

Related

Facebook Graph API not returning email

I have the following code:
$fb = new Facebook([
'app_id' => $appId,
'app_secret' => $appSecret,
'default_graph_version' => 'v2.9',
]);
$oAuth2Client = $fb->getOAuth2Client();
$tokenMetaData = $oAuth2Client->debugToken($accessToken);
dump($tokenMetaData);
$graphUser = $fb->get('/me?fields=first_name,last_name,email', $accessToken)->getGraphUser()->asArray();
dump($graphUser);
The output for the above is the following:
$metaData:
[
"app_id" => "..."
"application" => "My App Name"
"expires_at" => "2017-07-01 11:40:09.000000"
"is_valid" => true
"issued_at" => "2017-05-02 11:40:09.000000"
"metadata" => array:2 [
"auth_type" => "rerequest"
"sso" => "ios"
]
"scopes" => array:2 [
0 => "email"
1 => "public_profile"
]
"user_id" => "102..."
]
}
$graphUser:
array:3 [
"first_name" => "John"
"last_name" => "Smith"
"id" => "102...",
]
As you can see, the scopes in $metaData clearly has email so it isn't a permission issue. Despite this, the graph user sometimes does not have the email (although in some cases it does).
Why is this and how can I solve this issue?
Add the fields you need to the URL of your request:
https://graph.facebook.com/me?fields=email,name
First check if your access token gets the user's permission for the email
https://graph.facebook.com/me/permissions?
access_token=(access-token)
&debug=all
if in the answer, this content does not appear:
{
"data": [
{
"permission": "email",
"status": "granted"
},
{
"permission": "public_profile",
"status": "granted"
}
]
}
Maybe in obtaining your access token I do not request mail ownership: Add scope=email to the request
https://www.facebook.com/v2.10/dialog/oauth?
client_id=(app-id)
&redirect_uri=(redirect-uri)
&scope=email
One possibility which i came across today is, if i am registering a account where i am not logging in using my email ID, may be using mobile number to login, and even in my profile primary email is not set or set to a mobile number, in this case facebook returns null for email.
I have updated the account and added email to primary email field under settings in facebook page, then i was able to get the email id from facebook.
Hope this helps someone.
Happy coding...
You have to check these steps.
check if your access token gets the user's permission for the email . for that login to your developer account of your app, then tools->graph api explorer then enable user's permission for the email.
check the facebook version, use the latest version.
add email scope in face book login script as below
app.get(
'/auth/facebook',
passport.authenticate('facebook', {
scope: ['user_location', 'email', 'user_friends']
})
);
or
FB.login(function(response) {
// your ajax script
},{
scope: 'email',
return_scopes: true
});
Goto your app > App review > Permissions and Features
Request Advanced access for email which should turn Standard Access into Advanced Access
I solved this issue by specifying the graph api version.
My requested fields first_name,last_name,email,picture
To url: https://graph.facebook.com/me
Did NOT return email.
To url: https://graph.facebook.com/v2.9/me
Did return email.
Hope this helps.

Mention user in post to page from app using graph api

I need to mention user in post to page from our app. We are sharing post to users wall with users permission and we can post same thing to our fanpage. But now we want to mention same username in our fanpage post.
My code :
$page_info = $GLOBALS["facebook"]->api("/page_id?fields=access_token");
if (!empty($page_info['access_token'])) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => '#[{userid}:1:{name}] message',
// name : 'sdfg',
'link' => 'https://www.facebook.com/jedwsefdtlc/app_id?ref=ts',
// description : 'To vote https://facebook.com/page/app_id',
// actions:{
// name : "Vote now",
// link: "https://www.facebook.com/page/app_id?ref=ts"
// },
// url:'https://upload.wikimedia.org/wikipedia/commons/e/ec/Ara_ararauna_Luc_Viatour.jpg',
// link:{
'picture' => "https://example.com/app_dev.php/../bundles/soweb/images/chefs/chef_img/100_chef.jpg",//,
'name'=> "AFDS",
'caption'=>"caption",
'description' =>"description"
);
I referred this link
But in post text displayed as it is. It is not tagging. Any help?

Facebook API post to friends wall

I'm building application to post statuses from authenticated users like that one.
The problem is the application posting from my profile on friends wall who authenticated the application from my profile.
I'm using Facebook API with CodeIgniter.
the code
$userId = $this->facebook->getUser();
$IDs = $this->facebook_data->get_IDs();
if(isset($_POST['link'])){
$args = array(
'from' => array('name'=>'Resala','id'=>'294357147348261'),
'application'=>array('name'=>'XXXX','id'=>'XXXXX'),
'name' =>'XXXX',
'message' => $_POST['status2'],
'link' => $_POST['link']
);
}else{
$args = array(
'from' => array('name'=>'Resala','id'=>'294357147348261'),
'application'=>array('name'=>'Resala','id'=>'XXXXX'),
'name' =>'XXXX',
'message' => $_POST['status1']
);
}
foreach($IDs->result() as $row){
$ID=$row->user_id;
$this->facebook->api("/$ID/feed", "post", $args);
}
The right docs for creating a post are:
http://developers.facebook.com/docs/reference/api/user/#posts
There you can see the params "from" and "application" don't exist and "name (can only be used if link is specified)".
It's not possible to post in the name of an app. You can only post in the name of an page (that may be assosiated with the app).
Therefor you have to use the page access token instead of the user access token.
See: http://developers.facebook.com/docs/reference/api/page/#page_access_tokens

Facebook Graph API: How to read the "likes" of a page?

I try to read the list of people who likes a page, but it looks like it is not possible with the Graph API or FQL. I found lots of entries of people trying it, but nobody found a solution. Is it possible?
Here I get the profile info, I see the field "likes" with the number 200
https://graph.facebook.com/162253007120080/
But when I try to read it, I get an empty data array?
https://graph.facebook.com/162253007120080/likes
Anybody an idea how to handle this?
HI,
here you go, streight out my tool box:
<?php
require_once('../library/Facebook.php');
$facebook = new Facebook(array(
'appId' => '123456789000000',
'secret' => 'asdf',
'cookie' => true,
));
$result = $facebook->api(array(
'method' => 'fql.query',
'query' => 'select fan_count from page where page_id = 1234567890;'
));
echo $result[0]['fan_count'];
?>
Call the graph api link. Extract the contents using json_decode.
The resulting data array isn't empty when you go there, it is:
{
"error": {
"message": "An access token is required to request this resource.",
"type": "OAuthException",
"code": 104
}
}
Which means you need an access token in order to access that LIKE data..

Send notification or post on wall

i have been going crazy and cant figure out how to make a script for my application that would allow the user to select a friend and send them a notification or post on their wall from my application.
I just need to notify their friend that they have been challenged to play a flash games, just a simple text with a link, i dont need anything fancy :D
Here is what i tried and it doesnt work :( no idea why.
$message = 'Watch this video!';
$attachment = array( 'name' => 'ninja cat', 'href' => 'http://www.youtube.com/watch?v=muLIPWjks_M', 'caption' => '{*actor*} uploaded a video to www.youtube.com', 'description' => 'a sneaky cat', 'properties' => array('category' => array( 'text' => 'pets', 'href' => 'http://www.youtube.com/browse?s=mp&t=t&c=15'), 'ratings' => '5 stars'), 'media' => array(array('type' => 'flash', 'swfsrc' => 'http://www.youtube.com/v/fzzjgBAaWZw&hl=en&fs=1', 'imgsrc' => 'http://img.youtube.com/vi/muLIPWjks_M/default.jpg?h=100&w=200&sigh=__wsYqEz4uZUOvBIb8g-wljxpfc3Q=', 'width' => '100', 'height' => '80', 'expanded_width' => '160', 'expanded_height' => '120')));
$action_links = array( array('text' => 'Upload a video', 'href' => 'http://www.youtube.com/my_videos_upload'));
$target_id = $user;
$facebook->api_client->stream_publish($message, $attachment, $action_links, $target_id);
UPDATE:
appinclude.php
$facebook->redirect('https://graph.facebook.com/oauth/authorize?client_id=132611566776827&redirect_uri=https://apps.facebook.com/gamesorbiter/&scope=publish_stream');
Error i get:
{
"error": {
"type": "OAuthException",
"message": "Invalid redirect_uri: The Facebook Connect cross-domain receiver URL (https://apps.facebook.com/gamesorbiter/) must be in the same domain or be in a subdomain of an application's base domain (gamesorbiter.com). You can configure the base domain in the application's settings."
}
}
Without the extra "s"(http) i get this error:
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
Please if you could post an example.
Also do i need extended permission to do that or if the user sends the message i dont need that ?
Thank You
Also do i need extended permission to
do that or if the user sends the
message i dont that ?
Yes, you need the offline_access and publish_stream extended permission from the users.
Update:
In your appinclude.php file, put code like this:
$facebook = new Facebook($appapikey, $appsecret);
$user = $facebook->require_login();
$facebook->redirect('https://graph.facebook.com/oauth/authorize?
client_id=[YOUR APP ID]&
redirect_uri=[YOUR APP URL]&
scope=publish_stream, offline_access');
Replace [YOUR APP ID] with application id that you can see from application settings where you created the site in Facebook Developers section. Also replace the [YOUR APP URL] with your app url.