检索仅限电子邮件地址的Facebook / Google+ / Linkedin个人资料照片

时间:2015-04-29 11:58:47

标签: php facebook download profile-picture

我需要什么

我需要自动查找&下载用户只知道他的电子邮件地址的个人资料图片最初,考虑到积极使用它的人数,我专注于Facebook。但是,他们的API似乎没有直接的支持。

这里有类似的问题: How to get a facebook user id from the login email address这已经过时了,目前的答案是“它已被弃用”/“它不可能”...... 编辑:我发现了更好的问题:Find Facebook user (url to profile page) by known email address(实际上解释了为什么以及何时不支持此功能)

一定有办法...

是什么让我认为这应该是可能的 Spokeo 以某种方式这样做: http://www.spokeo.com/email-search/search?e=beb090303%40hotmail.com

有一些服务/ API提供这种功能:
Clearbit
Pipl
......但我没有找到任何免费的东西。

替代

如果有一些解决方法或不同的方法,而不是使用Facebook的API来实现这一目标,我想知道。如果Facebook在这里真的完全没有希望,那么这些组合: Google + Linkedin 和/或 Gravatar 可以做到。

我的第一次(原创)尝试:

拥有Facebook的用户名或用户ID后,可以轻松构建URL以下载图片。所以我试图使用带有/search Graph API的电子邮件来查找Facebook的用户ID:
https://graph.facebook.com/search?q=beb090303@hotmail.com&type=user&access_token=TOKEN

不幸总是以结尾“需要用户访问令牌才能请求此资源。”

使用FB PHP API + FB App ID&秘密

我也尝试了这一点:首先我使用应用ID和密码检索access_token,然后我尝试将其用作/search请求curl的一部分:

function post_query_url($url, $data) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $res = curl_exec($ch);
    curl_close($ch);
    return $res;
}

function get_query_url($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $ret = curl_exec($ch);
    curl_close($ch);
    return $ret;
}

function get_retrieve_app_access_token($app_id, $secret) {
    $url = 'https://graph.facebook.com/oauth/access_token?client_id='.$app_id.'&client_secret='.$secret.'&grant_type=client_credentials';
    $res = get_query_url($url);
    if (!empty($res)) {
        $tokens = explode('=', $res);
        if (count($tokens) == 2)
            return $tokens[1];
    }
    return null;
}

function post_retrieve_app_access_token($app_id, $secret) {
    $url = 'https://graph.facebook.com/oauth/access_token';
    $data = 'client_id='.$app_id.'&client_secret='.$secret.'&grant_type=client_credentials';
    $res = post_query_url($url, $data);
    if (!empty($res)) {
        $tokens = explode('=', $res);
        if (count($tokens) == 2)
            return $tokens[1];
    }
    return null;
}

function get_id_from_email($email, $accessToken) {
    $url = 'https://graph.facebook.com/search?q='.urlencode($email).'&type=user&access_token='.$accessToken;
    $res = get_query_url($url);
    if (!empty($res)) {
        return $res;
    }
    return null;
}

echo 'Retrieving token...<br>';

$token = post_retrieve_app_access_token('MY_APP_ID', 'SECRET');
echo 'Retrieved token: ' . $token . '<br>';

echo 'Retrieving user ID...<br>';
$id = get_id_from_email('beb090303@hotmail.com', $token);
echo 'Retrieved ID: ' . $id . '<br>';

输出如下内容:

Retrieving token...
Retrieved token: 367458621954635|DHfdjCnvO243Hbe1AFE3fhyhrtg
Retrieving user ID...
Retrieved ID: {"error":{"message":"A user access token is required to request this resource.","type":"OAuthException","code":102}}

其他信息

由于它要求“用户访问令牌”,我还试图访问Facebook的Graph Explorer:https://developers.facebook.com/tools/explorer/ 让它为我生成访问令牌并查询: search?q=beb090303@hotmail.com&type=user&debug=all 那个结束于:

{
  "error": {
    "message": "(#200) Must have a valid access_token to access this endpoint", 
    "type": "OAuthException", 
    "code": 200
  }
}

...所以Facebook在这里似乎有点无望。

4 个答案:

答案 0 :(得分:8)

有一个错误:Can't search for user by email after July 2013 Breaking Changes已被关闭为“按设计”官方回复:

  

“在2013年7月10日删除了将电子邮件地址传入”用户“搜索类型的功能。此搜索类型仅返回与用户名称匹配的结果(包括备用名称)”< / em>~JosephTuấnAnhPhan(脸书团队)

所以可能没有来自Graph API的直接支持。

我已经尝试了Graph API Explorer,您可以尝试使用某些FQL(只需要选择版本2.0,因为不再支持新版本),遗憾的是查询如下:

SELECT uid, name FROM user where email = 'some.email@gmail.com'

给出:

"error": {
  "message": "(#604) Your statement is not indexable. The WHERE clause must contain 
   an indexable column. Such columns are marked with * in the tables linked from
   http://developers.facebook.com/docs/reference/fql ", 
  "type": "OAuthException", 
  "code": 604
}

reference for table user表明uid只能使用third_party_idWHERE

答案 1 :(得分:2)

您应该需要访问令牌以及用户的Facebook ID。不知道他们无法获得他们的个人资料照片

答案 2 :(得分:0)

我认为Spokeo可能与Facebook达成协议以访问数据?我不会感到惊讶。

无论如何,如果您在个人资料中,您可以在HTML中搜索profile_id。这是一个黑客,不确定它是否有效。

答案 3 :(得分:0)

You could always allow people to comment by logging in with their g+/facebook/whatever account (requires you to do something OpenID-like, though); if they've logged in, you should be able to get the facebook uid.

Also, there's something called libravatar, which allows people to associate pictures with their OpenID or email address (and which falls back to gravatar if they haven't configured anything specifically for libravatar); using that should give you more photos than if you stick to "just" gravatar.