25

JanRain の PHP OpenID ライブラリを使用しています。SReg 拡張機能を使用したサンプル スクリプトが付属しています。しかし、私はそれをGoogleで動作させたい(実際には認証で動作します)が、Googleは追加データにSRegの代わりにAX(属性交換)を使用します. 何らかの理由で、JanRain のライブラリはサンプル スクリプトで AX サポートが欠落しており、AX スクリプトのコード コメントは私の理解外ですが、SReg スクリプトのコメントは 1-2-3 として明確です。

あまり苦労せずにAXを実装する方法を知っている人はいますか?

4

2 に答える 2

45

同じ問題に遭遇しました。AX.php を少し掘り下げて作業を開始しました。バグを探したり、基本を超えてテストしたり、Google 以外でテストしたりしていません。これはきれいではありません。エラー処理などが必要です。しかし、これで始められるはずです。堅牢なものがあれば更新を投稿します...

最初に投げる...

//  oid_request.php

// Just tested this with/for Google, needs trying with others ...
$oid_identifier = 'https://www.google.com/accounts/o8/id';

// Includes required files
require_once "Auth/OpenID/Consumer.php";
require_once "Auth/OpenID/FileStore.php";
require_once "Auth/OpenID/AX.php";

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

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

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

// Create an authentication request to the OpenID provider
$auth = $consumer->begin($oid_identifier);

// Create attribute request object
// See http://code.google.com/apis/accounts/docs/OpenID.html#Parameters for parameters
// Usage: make($type_uri, $count=1, $required=false, $alias=null)
$attribute[] = Auth_OpenID_AX_AttrInfo::make('http://axschema.org/contact/email',2,1, 'email');
$attribute[] = Auth_OpenID_AX_AttrInfo::make('http://axschema.org/namePerson/first',1,1, 'firstname');
$attribute[] = Auth_OpenID_AX_AttrInfo::make('http://axschema.org/namePerson/last',1,1, 'lastname');

// Create AX fetch request
$ax = new Auth_OpenID_AX_FetchRequest;

// Add attributes to AX fetch request
foreach($attribute as $attr){
    $ax->add($attr);
}

// Add AX fetch request to authentication request
$auth->addExtension($ax);

// Redirect to OpenID provider for authentication
$url = $auth->redirectURL('http://localhost:4001', 'http://localhost:4001/oid_catch.php');
header('Location: ' . $url);

...そしてキャッチする

<?php

//  oid_catch.php

// Includes required files
require_once "Auth/OpenID/Consumer.php";
require_once "Auth/OpenID/FileStore.php";
require_once "Auth/OpenID/AX.php";

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

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

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

// Create an authentication request to the OpenID provider
$auth = $consumer->complete('http://localhost:4001/oid_catch.php');

if ($response->status == Auth_OpenID_SUCCESS) {
    // Get registration informations
    $ax = new Auth_OpenID_AX_FetchResponse();
    $obj = $ax->fromSuccessResponse($response);

    // Print me raw
    echo '<pre>';
    print_r($obj->data);
    echo '</pre>';
    exit;


} else {
  // Failed
}

それらが基本のはずです...

于 2009-08-11T12:03:07.490 に答える
4

リクエストの半分は機能していますが、キャッチで失敗しています。

上記の行は

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

なれ

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

それ以外の場合、応答オブジェクトはどこから来るのですか? URL を確認するための応答で openid.current_url が返されませんか?

于 2009-08-14T02:02:24.423 に答える