获取用户凭据的OpenId调用返回空数组

时间:2011-08-01 17:36:54

标签: openid authentication

我已经用2个脚本设置了OpenId:

1) try_auth.php - initially invoked
2) finish_auth.php - meant to finish up the request

这是try_auth.php脚本:

<?php
// include files
  require_once "Auth/OpenID/Consumer.php";
  require_once "Auth/OpenID/FileStore.php";
  require_once "Auth/OpenID/SReg.php";

  // start session (needed for YADIS)
  session_start();

  $path = "https://www.google.com/accounts/o8/id";

  // create file storage area for OpenID data
  $store = new Auth_OpenID_FileStore('./oid_store');

  // create OpenID consumer
  $consumer = new Auth_OpenID_Consumer($store);


  // begin sign-in process
  // create an authentication request to the OpenID provider
  $auth = $consumer->begin("https://www.google.com/accounts/o8/id");
  if (!$auth) 
  {
    echo "<p>ERROR: Please enter a valid OpenID.</p>";
  }

  // create request for registration data
  $sreg = Auth_OpenID_SRegRequest::build(array('email', 'fullname', 'dob', 'language'), array('nickname'));

  if (!$sreg) 
  {
    die("ERROR: Unable to build Simple Registration request");
  }

  $auth->addExtension($sreg);  

  // redirect to OpenID provider for authentication
  $url = $auth->redirectURL('http://www.comehike.com/', 'http://www.comehike.com/auth/finish_auth.php');
  header('Location: ' . $url);  
?>

这是finish_auth.php脚本:

<?php

error_reporting(E_ERROR);
// include files
require_once "Auth/OpenID/Consumer.php";
require_once "Auth/OpenID/FileStore.php";
require_once "Auth/OpenID/SReg.php";


// start session
session_start();

// create store  
$store = new Auth_OpenID_FileStore('./oid_store');

// create consumer
$consumer = new Auth_OpenID_Consumer($store);

$response = $consumer->complete('http://www.comehike.com/auth/finish_auth.php');

echo $response->status;

// set session variable depending on authentication result
if ($response->status == Auth_OpenID_SUCCESS) 
{
  $_SESSION['OPENID_AUTH'] = true;

  // get registration information
  $sreg = new Auth_OpenID_SRegResponse();
  $obj = $sreg->fromSuccessResponse($response);

  echo '<p>Obj:</p>';

  $data = $obj->contents(); 
var_dump($data);
    echo '<p>Data:</p>';  
  var_dump($data);

  if (isset($data['email'])) 
  {
    error_log("..................email: ".$data['email']);
  }  
}

// redirect to restricted application page
//header('Location: restricted.php');  
?>

我正在使用本教程进行设置:http://devzone.zend.com/article/3581

基本上,一切正常,除了页面没有被重定向到谷歌登录,用户数据数组总是空的。

非常感谢任何有关出错的帮助或建议!

1 个答案:

答案 0 :(得分:2)

您必须使用ax(属性交换消息),请参阅:Example usage of AX in PHP OpenID

并修改

$auth = $consumer->complete('http://localhost:4001/oid_catch.php);

$response = $consumer->complete('http://localhost:4001/oid_catch.php);