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

早过忘川 提交于 2019-12-26 03:55:28

问题


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
}

回答1:


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.



来源:https://stackoverflow.com/questions/16014307/how-to-get-e-mail-when-logging-into-canvas-application-using-fb-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!