使用Janrain OpenID库从OpenID提供商处获取电子邮件地址

时间:2011-09-09 14:05:26

标签: php zend-framework openid janrain

当我使用Google登录StackOverflow时,收到以下消息:

  

Stackoverflow.com要求您提供Google帐户example@gmail.com

中的一些信息      

•电子邮件地址:example@gmail.com

但是,在我自己的网站上,当我使用OpenID登录时,我无法要求提供电子邮件地址。相反,我收到了这条消息:

  

您使用Google帐户example@gmail.com

登录example.com

我也发现很难理解我需要在哪个步骤申请电子邮件地址。以下是我认为应该构建步骤的代码:

/**
 * Authenticates the given OpenId identity.
 * Defined by Zend_Auth_Adapter_Interface.
 *
 * @throws Zend_Auth_Adapter_Exception If answering the authentication query is impossible
 * @return Zend_Auth_Result
 */
public function authenticate() {
    $id = $this->_id;
    $consumer = new Auth_OpenID_Consumer($this->_storage);

    if (!empty($id)) {
        $authRequest = $consumer->begin($id);

        if (is_null($authRequest)) {
            return new Zend_Auth_Result(
                Zend_Auth_Result::FAILURE,
                $id,
                array("Authentication failed", 'Unknown error')
            );
        }

        if (Auth_OpenID::isFailure($authRequest)) {
            return new Zend_Auth_Result(
                Zend_Auth_Result::FAILURE,
                $id,
                array("Authentication failed", "Could not redirect to server: " . $authRequest->message)
            );
        }

        $redirectUrl = $authRequest->redirectUrl($this->_root, $this->_returnTo);

        if (Auth_OpenID::isFailure($redirectUrl)) {
            return new Zend_Auth_Result(
                Zend_Auth_Result::FAILURE,
                $id,
                array("Authentication failed", $redirectUrl->message)
            );
        }

        Zend_OpenId::redirect($redirectUrl);
    } else {
        $response = $consumer->complete(Zend_OpenId::selfUrl());

        switch($response->status) {
            case Auth_OpenID_CANCEL:
            case Auth_OpenID_FAILURE:
                return new Zend_Auth_Result(
                    Zend_Auth_Result::FAILURE,
                    null,
                    array("Authentication failed. " . @$response->message)
                );
                break;
            case Auth_OpenID_SUCCESS:
                return $this->_constructSuccessfulResult($response);
                break;
        }
    }
}

这似乎应该是如此明显......但是我很难用谷歌搜索它并梳理代码来弄清楚它。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

您可以使用Zend Simple Registration Extension

索取电子邮件地址
$sreg = new Zend_OpenId_Extension_Sreg(array(
    'nickname'=>true,
    'email'=>false,
    'fullname'=>false), null, 1.1);
$consumer = new Zend_OpenId_Consumer();
if (!$consumer->login($_POST['openid_identifier'],
                      'example-6_3.php',
                      null,
                      $sreg)) {
    die("OpenID login failed.");
}

如果您想使用Janrain库,可以像这样添加对请求的扩展名:

$sreg_request = Auth_OpenID_SRegRequest::build(
                                 // Required
                                 array('nickname'),
                                 // Optional
                                 array('email'));

if ($sreg_request) {
    $authRequest->addExtension($sreg_request);
}

查看消费者示例:https://github.com/openid/php-openid/blob/master/examples/consumer/