php - Retrieving Facebook / Google+ / Linkedin profile picture having email address only -
what need
i need automatically find & download profile picture user knowing email address only. originally, focused on facebook considering amount of people actively using it. however, there seem no direct support api anymore.
there similar question here: how facebook user id login email address quite outdated , current answers there "it's deprecated" / "it's not possible"... edit: i've found better question: find facebook user (url profile page) known email address (where explained why , since when feature isn't supported)
there must way...
what makes me think should possible spokeo somehow doing it: http://www.spokeo.com/email-search/search?e=beb090303%40hotmail.com
there services / apis offering kind of feature:
clearbit
pipl
...but haven't found free.
alternatives
if there workaround or different approach using facebook's api achieve this, know. if facebook hopeless here, combination of these: google+, linkedin and/or gravatar do.
my first (original) attempt:
once have facebook's username or user id, it's easy build url download picture. trying facebook's user ids using emails /search
graph api:
https://graph.facebook.com/search?q=beb090303@hotmail.com&type=user&access_token=token
which unfortunatelly ends "a user access token required request resource."
using fb php api + fb app id & secreti've tried this: @ first retrieve access_token
using app id , secret , i'm trying use part of /search
request 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>';
outputs like:
retrieving token... retrieved token: 367458621954635|dhfdjcnvo243hbe1afe3fhyhrtg retrieving user id... retrieved id: {"error":{"message":"a user access token required request resource.","type":"oauthexception","code":102}}
other info
since it's asking "user access token", i've tried go facebook's graph explorer: https://developers.facebook.com/tools/explorer/ let generate access token me , queried: search?q=beb090303@hotmail.com&type=user&debug=all
1 ends with:
{ "error": { "message": "(#200) must have valid access_token access endpoint", "type": "oauthexception", "code": 200 } }
...so facebook seems kinda hopeless here.
that's why gravatar exists , why people use gravatar, users know public profile image bind e-mail address , know change it.
your app can have possibility users upload own profile image , fallback gravatar.
if try extract image facebook or google+, might freak users out , harder them know service got profile image from.
using gravatar in php simple this:
<?php $email = "email@server.com"; $default = ""; // absolute url default image goes here or leave empty 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); ?>
apart that, can use facebook and/or google+ external login providers users can grant application access profile information.
Comments
Post a Comment