如何在OpenId登录后获取用户的凭据?

时间:2011-07-31 18:56:15

标签: php openid google-openid

我有一些使用OpenId的PHP代码,并在该用户验证他们的Google ID用于登录我的网站后运行。

我需要能够提取基本凭据,例如 - 名字 - 姓 - 电子邮件 - 如果可能的话,lat / lng ..如果不是的话。

我有这个代码,但我不知道如何获取名称/电子邮件等。

<?php
require_once "common.php";
session_start();

function escape($thing) 
{
    return htmlentities($thing);
}

function run() 
{
    $consumer = getConsumer();
    var_dump($consumer); 
    echo '<p>test 2</p>';   

    // Complete the authentication process using the server's
    // response.
    $return_to = getReturnTo();

        var_dump($return_to); 
    echo '<p>test 3</p>';    
    $response = $consumer->complete($return_to);

        var_dump($response); 
    echo '<p>test 4</p>';     

    // Check the response status.
    if ($response->status == Auth_OpenID_CANCEL) 
    {
        // This means the authentication was cancelled.
        $msg = 'Verification canceled.';
         echo '<p>Canceled</p>';  
    } 
    else 
    if ($response->status == Auth_OpenID_FAILURE) 
    {
        echo '<p>Open Id Failure</p>';  
        // Authentication failed; display the error message.
        $msg = "OpenID authentication failed: " . $response->message;
    } 
    else 
    if ($response->status == Auth_OpenID_SUCCESS) 
    {
            echo '<p>SUCCESS</p>';  
        // This means the authentication succeeded; extract the
        // identity URL and Simple Registration data (if it was
        // returned).
        $openid = $response->getDisplayIdentifier();

        var_dump($openid); 

        $esc_identity = escape($openid);



        $success = sprintf('You have successfully verified ' .
                           '<a href="%s">%s</a> as your identity.',
                           $esc_identity, $esc_identity);

        if ($response->endpoint->canonicalID) 
        {
            $escaped_canonicalID = escape($response->endpoint->canonicalID);
            $success .= '  (XRI CanonicalID: '.$escaped_canonicalID.') ';
        }

        $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);

        $sreg = $sreg_resp->contents();

        var_dump($sreg); 
    echo '<p>test sreg</p>';         

        if (@$sreg['email']) 
        {
            $success .= "  You also returned '".escape($sreg['email']).
                "' as your email.";

                echo $success;
        }
        else
        {
            echo '<p>Not email success</p>';
        }


        if (@$sreg['nickname']) 
        {
            $success .= "  Your nickname is '".escape($sreg['nickname']).
                "'.";

            echo $success;
        }
        else
        {
                    echo '<p>Not nickname success</p>';
        }

        if (@$sreg['fullname']) 
        {
            $success .= "  Your fullname is '".escape($sreg['fullname']).
                "'.";

                                echo $success;
        }
        else
        {
                        echo '<p>Not full name success</p>';
        }

        $pape_resp = Auth_OpenID_PAPE_Response::fromSuccessResponse($response);

        if ($pape_resp) 
        {
            if ($pape_resp->auth_policies) 
            {
                $success .= "<p>The following PAPE policies affected the authentication:</p><ul>";

                foreach ($pape_resp->auth_policies as $uri) 
                {
                    $escaped_uri = escape($uri);
                    $success .= "<li><tt>$escaped_uri</tt></li>";
                }

                $success .= "</ul>";
            } 
            else 
            {
                $success .= "<p>No PAPE policies affected the authentication.</p>";
            }

            if ($pape_resp->auth_age) 
            {
                $age = escape($pape_resp->auth_age);
                $success .= "<p>The authentication age returned by the " .
                    "server is: <tt>".$age."</tt></p>";
            }

            if ($pape_resp->nist_auth_level) 
            {
                $auth_level = escape($pape_resp->nist_auth_level);
                $success .= "<p>The NIST auth level returned by the " .
                    "server is: <tt>".$auth_level."</tt></p>";
            }
    } 
    else 
    {
            $success .= "<p>No PAPE response was sent by the provider.</p>";
    }
}
echo '<p>End of script - finish auth</p>';
    include 'http://www.comehike.com/index.php';


                             // $_SESSION['user_id'] = $user_id;
                             // $_SESSION['user_email']  = $row['user_email'];
                             // $_SESSION['user_lat']  = $row['lat'];
                             // $_SESSION['user_lng']  = $row['lng'];
                             // $_SESSION['first_name'] = $row['first_name'];
}

run();


?>

以下是对此事的用户体验的测试:http://www.comehike.com/account/member_home.php

1 个答案:

答案 0 :(得分:0)

我相信这已在此解决:Example usage of AX in PHP OpenID

您必须为Google和Yahoo提供属性交换(AX)。您应该能够同时使用sreg和ax来获取所需的信息。