csga5000
csga5000

Reputation: 4151

FacebookSDK search by username/pagename

I'm trying to access public information for a user based on their username/pagename.

I'm doing this:

$uname = 'csga5000';

$session = new Facebook\Facebook([
    'app_id' => self::$FACEBOOK_APP_ID,
    'app_secret' => self::$FACEBOOK_APP_SECRET,
    'default_graph_version' => 'v2.2'
]);

try {
    // Returns a `Facebook\FacebookResponse` object
    $response = $session->get('/search?q='+$uname+'&type=user');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
    print_r($e);
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
}

But the exception is thrown, here is the output:

Graph returned an error: (#803) Some of the aliases you requested do not exist: v2.20

Alternatively(Got this from looking at the source code)

$response = $session->sendRequest('GET','/search',['q'=>$uname,'type'=>'user']);

Returns "Graph returned an error: An access token is required to request this resource." I stole a reference from somewhere using app id and secret as the token in this format:

$response = $session->sendRequest('GET','/search',['q'=>$uname,'type'=>'user'], self::$FACEBOOK_APP_ID.'|'.self::$FACEBOOK_APP_SECRET);

This throws: "Graph returned an error: A user access token is required to request this resource."

I don't understand why a USER access token is required :/

Anyone know how to search for a user based on username in php? I've seen other examples that were slightly outdated, but I was pretty much the same as what they claim worked.

Edit: The information we want to acquire is primarily follower count.

Upvotes: 0

Views: 193

Answers (2)

csga5000
csga5000

Reputation: 4151

It seems to be the case that it's not possible(at the time) to access more than a user's name publicly. OAuth is required to access the public profile (which is a default permission).

Using the graph api explorer you can verify this using the endpoint /{user-id} and trying to get additional information for a user with whatever id.

The return is like this:

{
    "name": "John Doe",
    "id": "100003216243221"
}

You can get first and last name as well, by appending:

?fields=name,first_name,last_name

But access to other information fails, even when public. (I only tested location, which clearly has an additional permission).

EDIT!

HOWEVER: If you wish to access a facebook page, some information is accessible.

This is some of the information returned if no fields are specified: about, id, can_post, category, check_ins, has_added_app, is_community_page, is_published, likes, link, location, name, phone, talking_about_count, username, website, were_here_count.

Using the endpoint:

{page-username-or-id}?fields=engagement,is_permanently_closed,emails,picture

Can retrieve other information fields (assuming they're public).

So code for this: Facebook sdk v4:

$session = FacebookSession::newAppSession();
$session->validate();

$ret = new FacebookRequest(
        $session, 'GET', '/'.$uname.'?fields=name,engagement,phone,link,is_community_page,is_permanently_closed,influences,hometown,general_info,emails,bio,birthday,description,location,username,likes,picture'
    );

$ret = $ret->execute()->getGraphObject()->asArray();

If you want to catch exceptions, you can look the thrown exceptions up: https://developers.facebook.com/docs/php/

This works for the same request for facebook sdk v5 (though, I don't know that the way I called send request is advised):

$uname = 'tbdsolutions5000';

$session = new Facebook\Facebook([
    'app_id' => self::$FACEBOOK_APP_ID,
    'app_secret' => self::$FACEBOOK_APP_SECRET,
    'default_graph_version' => 'v2.2'
]);
try {

    $response = $session->sendRequest('GET','/'.$uname,[
            //Optionally:
            //'fields' => 'comma, separated, fields'
        ],
        self::$FACEBOOK_APP_ID.'|'.self::$FACEBOOK_APP_SECRET
    );
} catch(Facebook\Exceptions\FacebookResponseException $e) {
    print_r($e);
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
}

$response = $response->getGraphObject()->asArray();

Upvotes: 0

andyrandy
andyrandy

Reputation: 74014

First of all, you can´t search for users by username anymore. Earlier this was possible by just using a simple API call: graph.facebook.com/username

This does not work anymore, and the Search API only allows to search for the name (first/last). You are not supposed to use the username anymore.

That being said, you need to authorize yourself and use a User Token, as the error message tells you. That´s just how it is, and you can read it in the docs too:

  • Searches across Page and Place objects requires an app access token.
  • All other endpoints require a user access token.

Source: https://developers.facebook.com/docs/graph-api/using-graph-api/v2.4#search

More information about Tokens:

Upvotes: 1

Related Questions