尝试在Google上使用php OpenID时出现501 HTTP错误

时间:2012-04-08 11:58:25

标签: php openid janrain http-error

我一直在尝试将openID身份验证作为我正在开发的项目中的消费者实现,而我还没有设法让这些示例按照我的意愿运行。

即使示例消费者完全适用于yahoo openid身份验证,但在尝试使用google openID时,在try_auth.php页面中失败并出现501 HTTP错误。

以下是try_auth.php(处理对实际openID提供程序的调用的页面)的代码:

<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
require_once "common.php";
session_start();

function getOpenIDURL() {
    // Render a default page if we got a submission without an openid
    // value.
    if (empty($_GET['openid_identifier'])) {
        $error = "Expected an OpenID URL.";
        include 'index.php';
        exit(0);
    }

    return $_GET['openid_identifier'];
}

function run() {
    $openid = getOpenIDURL();
    $consumer = getConsumer();

    // Begin the OpenID authentication process.
    $auth_request = $consumer->begin($openid);

    // No auth request means we can't begin OpenID.
    if (!$auth_request) {
        displayError("Authentication error; not a valid OpenID.");
    }

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

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

    $policy_uris = null;
    if (isset($_GET['policies'])) {
        $policy_uris = $_GET['policies'];
    }

    $pape_request = new Auth_OpenID_PAPE_Request($policy_uris);
    if ($pape_request) {
        $auth_request->addExtension($pape_request);
    }

    // Redirect the user to the OpenID server for authentication.
    // Store the token for this authentication so we can verify the
    // response.

    // For OpenID 1, send a redirect.  For OpenID 2, use a Javascript
    // form to send a POST request to the server.
    if ($auth_request->shouldSendRedirect()) {
        $redirect_url = $auth_request->redirectURL(getTrustRoot(),
                                                   getReturnTo());

        // If the redirect URL can't be built, display an error
        // message.
        if (Auth_OpenID::isFailure($redirect_url)) {
            displayError("Could not redirect to server: " . $redirect_url->message);
        } else {
            // Send redirect.
            header("Location: ".$redirect_url);
        }
    } else {
        // Generate form markup and render it.
        $form_id = 'openid_message';
        $form_html = $auth_request->htmlMarkup(getTrustRoot(), getReturnTo(),
                                               false, array('id' => $form_id));

        // Display an error if the form markup couldn't be generated;
        // otherwise, render the HTML.
        if (Auth_OpenID::isFailure($form_html)) {
            displayError("Could not redirect to server: " . $form_html->message);
        } else {
            print $form_html;
        }
    }
}

run();

?>

我注意到的另一个想法是,在我的Windows开发盒(Apache 2.2.6独立,而不是XAMPP,PHP 5.3.8)上,一切运行顺利,雅虎和谷歌都执行openID身份验证,没有任何问题。

任何人都知道可能出现的问题?

提前致谢。

1 个答案:

答案 0 :(得分:0)

经过一些反复试验,我得出的结论是501错误的发生是由于Google openID url作为查询字符串传递给页面(对于表单方法“get”)要么作为postdata(对于表单方法“post”) “)。特别是,我使用的网址是

  

https://www.google.com/accounts/o8/id

最后一部分(“id”)触发501错误。如果我使用

  

https://www.google.com/accounts/o8/id/

未触发错误。好吧,因为这两个是相同的网址,我将使用第二个。我仍然很好奇为什么会发生这种情况。