2012-04-08 103 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,我將使用第二個。儘管如此,我仍然很好奇。

相關問題