繁体   English   中英

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

[英]Get email address from OpenID provider using Janrain OpenID library

当我使用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;
        }
    }
}

看来应该是如此明显...但是我很难用Google搜寻并梳理代码以找出答案。 任何帮助将不胜感激!

您可以使用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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM