FacebookSDK 搜索 username/pagename

FacebookSDK search by username/pagename

我正在尝试根据 username/pagename.

访问用户的 public 信息

我正在这样做:

$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;
}

但是抛出了异常,这里是输出:

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

或者(通过查看源代码得到这个)

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

Returns "Graph returned an error: An access token is required to request this resource." 我从某处使用应用程序 ID 和秘密作为令牌以这种格式窃取了引用:

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

这抛出:"Graph returned an error: A user access token is required to request this resource."

我不明白为什么需要用户访问令牌:/

有人知道如何根据 php 中的用户名搜索用户吗?我看过其他稍微过时的示例,但我与他们声称有效的方法几乎相同。

编辑: 我们要获取的信息主要是关注人数。

首先,您不能再通过用户名搜索用户了。早些时候,这可以通过简单的 API 调用来实现:graph.facebook.com/username

这不再有效,搜索 API 只允许搜索名称 (first/last)。您不应再使用该用户名。

也就是说,您需要授权自己并使用用户令牌,正如错误消息告诉您的那样。就是这样,您也可以在文档中阅读它:

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

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

关于代币的更多信息:

似乎(当时)不可能访问超过用户名的 publicly。需要 OAuth 才能访问 public 配置文件(这是默认权限)。

使用图形 api 资源管理器,您可以使用端点 /{user-id} 验证这一点,并尝试为具有任何 ID 的用户获取更多信息。

return是这样的:

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

您也可以通过追加获得名字和姓氏:

?fields=name,first_name,last_name

但即使 public 也无法访问其他信息。 (我只测试了位置,显然有额外的权限)。

编辑!

但是: 如果您想访问 facebook 页面,可以访问一些信息。

如果未指定字段,这是 return 编辑的一些信息:关于、id、can_post、类别、check_ins、has_added_app、is_community_page, is_published, 喜欢, link, 位置, 姓名, phone, talking_about_count, 用户名, 网站, were_here_count.

使用端点:

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

可以检索其他信息字段(假设它们是 public)。

为此编写代码: 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();

如果你想捕获异常,可以查看抛出的异常:https://developers.facebook.com/docs/php/

这适用于 facebook sdk v5 的相同请求(不过,我不知道我调用发送请求的方式是否被建议):

$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();