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

Lih*_*ihO 34 php facebook download profile-picture

我需要的

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

这里有类似的问题: 如何从登录电子邮件地址获取一个facebook用户ID已经过时了,目前的答案是"它已被弃用"/"它不可能"...编辑:我发现了更好的问题:通过已知的电子邮件地址查找Facebook用户(网址到个人资料页面)(实际上解释了为什么以及何时不支持此功能)

一定有办法......

让我觉得这应该是可能的是Spokeo在某种程度上这样做:http: //www.spokeo.com/email-search/search?e = beb090303%40hotmail.com

有一些服务/原料药提供了这种功能:
Clearbit
PIPL
......但我还没有发现任何自由.

备择方案

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


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

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

不幸的是总是以"请求此资源需要用户访问令牌"结束.

使用FB PHP API + FB App ID和Secret

我也试过这个:首先我access_token使用应用程序ID和秘密检索,然后我尝试将其用作/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>';
Run Code Online (Sandbox Code Playgroud)

输出如下:

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}}
Run Code Online (Sandbox Code Playgroud)

其他信息

由于它要求"用户访问令牌",我还试图访问Facebook的图形资源管理器: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
  }
}
Run Code Online (Sandbox Code Playgroud)

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

huy*_*itw 17

这正是为什么Gravatar存在以及为什么人们使用Gravatar,用户知道哪些公共配置文件图像绑定到哪个电子邮件地址,他们知道在哪里更改它.

您的应用可以让用户上传自己的个人资料图片并回退到Gravatar.

如果您只是尝试从Facebook或Google+中提取图片,可能会让您的用户感到不安,而且他们也很难知道您的服务从哪里获得了个人资料图片.

在PHP中使用Gravatar就像这样简单:

<?php
$email = "email@server.com";
$default = ""; // absolute url to default image goes here or leave empty for default gravatar image
$size = 200;

$grav_url = "http://www.gravatar.com/avatar/" . md5(strtolower(trim($email))) . "?d=" . urlencode($default) . "&s=" . $size;

header("content-type: image/jpeg");
echo file_get_contents($grav_url);
?>
Run Code Online (Sandbox Code Playgroud)

除此之外,您还可以使用Facebook和/或Google+作为外部登录提供商,用户可以授予您的应用程序访问其个人资料信息的权限.

  • 如果没有与电子邮件地址关联的个人资料图片,您还可以通过`?d=404` 使 Gravatar 返回 404。这有助于确定用户是否自定义了他们的 Gravatar 图像。 (2认同)

Lih*_*ihO 8

有一个错误:无法通过电子邮件在2013年7月之后通过电子邮件搜索用户已通过官方回复关闭的"按设计" 更改:

"在2013年7月10日删除了将电子邮件地址传递到"用户"搜索类型的功能.此搜索类型仅返回与用户名称匹配的结果(包括替代名称)" ~JosephTuấnAnhPhan(Facebook球队)

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

我已经尝试了Graph API资源管理器,您可以尝试使用一些FQL(只需要选择版本2.0,因为不再支持更新的版本),不幸的是查询如下:

SELECT uid, name FROM user where email = 'some.email@gmail.com'
Run Code Online (Sandbox Code Playgroud)

得到:

"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
}
Run Code Online (Sandbox Code Playgroud)

表用户的参考显示只有uidthird_party_id可以使用WHERE.