如何正确使用facebook范围来获取用户电子邮件?

时间:2011-07-21 17:40:27

标签: php facebook

我需要在我的网站上使用Facebook登录时检索用户的电子邮件。 Facebook在其登录URL中有一个scope参数,用于请求这些扩展权限。我究竟要添加哪些代码才能正确设置范围以包含电子邮件?

/**
* Get a Login URL for use with redirects. By default, full page redirect is
* assumed. If you are using the generated URL with a window.open() call in
* JavaScript, you can pass in display=popup as part of the $params.
*
* The parameters:
* - redirect_uri: the url to go to after a successful login
* - scope: comma separated list of requested extended perms
*
* @param Array $params provide custom parameters
* @return String the URL for the login flow
*/
public function getLoginUrl($params=array()) {
 $this->establishCSRFTokenState();
 $currentUrl = $this->getCurrentUrl();
 return $this->getUrl(
   'www',
   'dialog/oauth',
   array_merge(array(
                 'client_id' => $this->getAppId(),
                 'redirect_uri' => $currentUrl, // possibly overwritten
                 'state' => $this->state),
               $params));
}

1 个答案:

答案 0 :(得分:3)

添加范围参数:

public function getLoginUrl($params=array()) {
 $this->establishCSRFTokenState();
 $currentUrl = $this->getCurrentUrl();
 return $this->getUrl(
   'www',
   'dialog/oauth',
   array_merge(array(
                 'client_id' => $this->getAppId(),
                 'redirect_uri' => $currentUrl, // possibly overwritten
                 'state' => $this->state,
                 'scope'=>'email'),
               $params));
}