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


Get email address from OpenID provider using Janrain OpenID library

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

Stackoverflow.com正在询问一些信息从你的谷歌帐户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;
        }
    }
}

这似乎应该是如此明显…但我很难在谷歌上搜索并梳理代码来找出它。任何帮助将非常感激!

您可以使用Zend简单注册扩展请求电子邮件地址

$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/