Facebook SDK PHP: return friends details - facebook-graph-api

I have a web service that servers Android, IOS and Windows Phone. Each app connects to Facebook using native SDK and passes the fb_user_id and access_token when performs API's requests. The API should retrieve the user and friends details and save them on MySQL.
The system works fine, except that it can't return friend's retionship, location and birthday.
$fb = new Facebook([
'appId' => my_app_id,
'secret' => my_app_secret
]);
$r = $fb->api("$fb_user_id/?fields=friends.fields(relationship,birthday,location)&access_token=$access_token");
var_dump( $r );
My app on Facebook has friends_relationships, friends_location and friends_birthday permissions. When we test using a Facebook user setted as developer, the app returns all the information, but when a non-developer uses we can't return those informations.
Any idea?

I figured what happened!
Facebook's SDK generates a token for /me. In PHP I was calling fb_user_id?fields=friends.fields(id,name,birthday,etc). But requests with /fb_user_id can't return info with a /me generated token.
It works when used directly on browser https://graph.facebook.com/me?fields=friends.fields(id,name,birthday,etc), but fails when called by Facebook's PHP class:
An active access token must be used to query information about the current user.
My solution:
$url = "https://graph.facebook.com/me?fields=id,name&access_token=$this->access_token";
$this->me = file_get_contents ( $url );
$json = json_decode($this->me, true);
For further information, checkout this answer. New ideas are welcome.

Related

Delete Facebook App with Graph API

I'm developing a Facebook authentication system using the Graph Api v2.8.
The website that makes the request requires the user's email.
Problem: the app is added on the user's Facebook regardless he unchecks or not the email permission option.
Is there any way of preventing the app being added on the user's facebook when he refuses to share his email?
If not, how can I remove the app from the user's facebook?
Thks in advance.
Here is one possible solution that solved the problem, by removing the app from the user's Facebook:
$response = $fb->get('/me?fields=first_name,last_name,email');
$userNode = $response->getGraphUser();
if (!isset($userNode['email']) || (isset($userNode['email']) && !$userNode['email'])) {
$fb->delete($userNode['id'] . '/permissions');
}
The line:
$fb->delete($userNode['id'] . '/permissions');
deletes the user app.

Laravel Socialite - get user details by token

I'am developing android application with facebook login and laravel REST API server. After user login on mobile, app get token which is sent to my server. On server I want get facebook user details by token.
Facebook SDK for PHP provides methods for that:
$session = new FacebookSession('token');
$me = (new FacebookRequest($session, 'GET', '/me',))->execute()->getGraphObject(GraphUser::className());
Can Laravel Socialite obtain user facebook details based on that token? Or just use that Facebook SDK?
I submitted a pull request to the repo which was accepted to the 2.o branch. You should be able to call Socialte::driver('facebook')->userFromToken($token) now.
https://github.com/laravel/socialite/pull/126
Cheers.
You can also extend your Socialite Facebook Provider.
For example;
Create a class as SocialConnect in App/Library folder
namespace App\Library;
use Laravel\Socialite\Two\FacebookProvider;
class SocialConnect extends FacebookProvider {
public function userFromToken($access_token)
{
$user = $this->mapUserToObject($this->getUserByToken($access_token));
return $user->setToken($access_token);
}
}
Then add these lines in boot method on your App/Providers/AppServiceProvider.php
$socialite = $this->app->make('Laravel\Socialite\Contracts\Factory');
$socialite->extend(
'SocialConnect',
function ($app) use ($socialite) {
$config = $app['config']['services.facebook'];
return $socialite->buildProvider(SocialConnect::class, $config);
}
);
Ready for use!
Socialite::driver('SocialConnect')->userFromToken($accessToken)
You can also add new methods as you wish in your SocialConnect class.
There is a method getUserByToken implemented in the Facebook Service Provider, however it's protected and only used internally to get the user details.
So you're better off using the Facebook SDK.

arguments when using the PHP Facebook API Graph library?

I am trying to pull some statuses in json format from Facebook's Graph API. Since i am using PHP, so I went for the following library.
https://github.com/facebook/facebook-php-sdk/blob/master/examples/example.php
I saw the example, and the class requires some arguments upon instantiation, and I just don't know what they are:
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '344617158898614',
'secret' => '6dc8ac871858b34798bc2488200e503d',
));
And also from the Graph API Explorer I see the access_token, but this PHP library doesn't mention it at all, I thought I need the access_token to make requests, don't I? I hope someone can clear things up for me.
Yes you require an access_token to query on behalf of user on Facebook, it can either be an App access token or User access token. If the user authorizes your app, with required permission which in your case would be read_stream permission, then you can obtain his access token and query for status updates. The default access token is your App access token which can access only public data.
Also I would suggest you to to over the PHP SDK documentation for getting started with it.

Does Facebook generate the same "offline_access" Access Token for the same user each time they authenticate?

I'm building a web application and I'm using OAuth to provide Facebook authentication. I've successfully generated an access_token and each time the user logs out/back in, I match their access_token to find their account. This works very well.
In addition, I'm building a Windows Phone 7 companion app. I've got the same process for authentication, using the Facebook SDK, but the access_token that is returned is different. This means I cannot match the account.
Since the logout/login to my web application appears to generate the same access_token repeatedly, I'm confused as to why the mobile app generates a different access token.
Can anyone tell me if the behaviour I'm expecting is correct? I've scoured the Facebook documentation, but can't find anything relevant. Maybe OAuth isn't the correct thing to do here.
Each time I logged in, the code would return a different access_code value. I needed to add a new field called "response_type" with a value of "code". This then redirected me to the login page and responded with the correct token, which I then exchanged for an access code.
To generate the URL, this was the code:
Facebook.FacebookOAuthClient client = new Facebook.FacebookOAuthClient();
client.AppId = "285539721457932";
client.AppSecret = "65233641cf71e6fa9fef5ecd7d802ebf";
client.RedirectUri = new Uri("http://www.imaybelate.com/Account/FacebookCallback");
url = client.GetLoginUrl(new Dictionary<string, object>()
{
{ "display", "wap" },
{ "response_type", "code" },
{ "permissions","offline_access"}
}).ToString();

FaceBook downloading photos using php-sdk

I am new to Facebook PHP SDK for the Graph API.
I have created an application on Facebook and I got the application id, secret ..etc.
I also got the access code with some permissions using the scope parameter.
Using that code I got the access token with offline access.
https: //graph.facebook.com/oauth/access_token?client_id=xxxxxxxxxx&redirect_uri=http://192.168.15.5/xxxx/facebook/&client_secret=xxxxx&grant_type=client_credentials.
Using this I got the access token.
Now my Question is that I want to access the user information such as photos and friend lists
I do not know if it is correct or not,
https://graph.facebook.com/me/friends?access_token=xxxxxxxxxxxxxxxxxxxxx
when I use this it says,
"{
"error": {
"type": "OAuthException",
"message": "An active access token must be used to query information about the current user."
}
}"
How can I access the user information, by using the access token of that user.
I want to access the user information from the application at anytime for the user who has allowed my application to access his information.
If you use the Facebook PHP SDK (see on github), you should not call these URL directly but only call functions of the SDK.
See this answer to see how to fetch and use the offline access token to make API calls : How to login with offline_access using the new Facebook PHP SDK 3.0 ?
Then to get the user friends and their photos, you will call :
$args = array("fields" => "name,picture");
$friends = $facebook->api('/me/friends', $args);
Then the array $friends['data'] will contain the URLs of the pictures of the friends :
Array(
[0] => Array(
[name] => Quentin Pleplé
[id] => 1536397056
[picture] => http://profile.ak.fbcdn.net/...jpg
)
[1] => ...
...
)
Hope that helps !