Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

facebook - How to get e-mail when logging into canvas application using fb graph?

I want to get the user email when the user logged into my facebook canvas application using only fb graph api?I dont want to use facebook php sdk as I can not handle it with this code(Actually,I tried a lot but failed)Plz help

$app_id = "xxxxxxx";

$canvas_page = "xxxxxxxx";

$auth_url = "http://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" . urlencode($canvas_page);

$signed_request = $_REQUEST["signed_request"];

list($encoded_sig, $payload) = explode('.', $signed_request, 2);

$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), TRUE);

if(empty($data["user_id"])) 
{
    echo("<script> top.location.href='" . $auth_url . "'</script>");
} 
else 
{
    echo ("Welcome User: " . $data["user_id"]);
    echo ("Welcome User: " . $data["email"]);   //i tried this
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The 'email' field can be found within the FB API's user object, but only if you have explicitly asked for email permissions. There are many ways to request this permission, e.g.

$param['scope'] = "email";
$login_url = $facebook->getLoginUrl($param);
echo 'Please <a href="' . $login_url . '">login.</a>';

will print a login url that authorizes email permissions. You can then retrieve an email through the user object, as in the following example:

$user_profile = $facebook->api('/me','GET');
echo "email: ".$user_profile['email'];

Note: There is no way for apps to obtain email addresses for a user's friends.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...