2012-04-08 88 views
0

我一直在尝试在我正在开发的项目中作为使用者实现openID身份验证,而且我还没有设法让示例正常工作,因为我想要。尝试使用php OpenID与Google时出现的HTTP错误

尽管示例消费者完美适用于yahoo openid身份验证,但在try_auth.php页面尝试使用Google openID时发生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开发框(阿帕奇2.2.6独立,不是XAMPP,PHP 5.3.8)一切运行顺利,雅虎和谷歌都没有任何问题地执行openID身份验证。

任何人有一个想法可能是错的什么?

在此先感谢。

+0

你能告诉我们一些代码 – Baba 2012-04-08 12:29:37

+0

这是janrain openid附带的示例消费者代码。我没有改变那里的一行代码。 – Yiangos 2012-04-08 16:03:41

+0

将'error_reporting(E_ALL);'和'ini_set('display_errors','On');''添加到您的代码并提取确切的PHP错误 – Baba 2012-04-08 16:05:17

回答

0

经过一些试验和错误,我得出结论认为,501错误发生是由于Google openID网址作为查询字符串(用于表单方法“get”)传递给页面或者作为postdata(用于表单方法“后“)。特别是,我所用的网址是

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

最后一部分(以下简称 “标识”)引发501错误。如果我使用

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

则不会触发错误。那么,因为这两个是等效的URL,我将使用第二个。尽管如此,我仍然很好奇。

相关问题