OpenId组件:使用CakePHP从Google获取属性

时间:2012-04-26 12:06:50

标签: cakephp components openid cakephp-2.0

我是否可以使用CakePHP和OpenID组件从Google成功获取OpenId用户属性(如姓名,电子邮件)?当我尝试添加所需参数时,我收到“您请求的页面无效”。

更多细节

组件:http://code.42dh.com/openid/

如果我不请求任何“属性”,它可以正常工作。我尝试添加必需/可选属性请求时,如下例所示,我收到Google的错误:“您请求的页面无效。”

示例(不适用于我):http://cakebaker.42dh.com/2008/02/12/using-the-openid-simple-registration-extension/

根据1来源,问题是:

  

错误实际上是由于不包括错误而引发的   openid.claimed_id和openid.identity参数,必须设置为   “http://specs.openid.net/auth/2.0/identifier_select”。有了这些设定,   我得到另一个错误,也可以通过填写来解决   openid.realm,与openid.return_to具有相同的值。

Google OpenID: the page you requested is invalid

代码

function openidlogin() {

    $realm = 'http://' . $_SERVER['HTTP_HOST'];
    $returnTo = $realm . '/users/openidlogin';


    $url = "https://www.google.com/accounts/o8/id";
    if ($this->RequestHandler->isPost() && !$this->Openid->isOpenIDResponse()) {
        try {
            $this->Openid->authenticate($url, $returnTo, $realm); // WORKS !!!
            $this->Openid->authenticate($url, 'http://'.$_SERVER['SERVER_NAME'].'/users/login', 'http://'.$_SERVER['SERVER_NAME'], array('email'), array()); // FAILS
        } catch (InvalidArgumentException $e) {
            $this->Session->setFlash("Error: Invalid OpenId");
        } catch (Exception $error) {
            $this->Session->setFlash("Error: " + $error->getMessage());
        }
    } elseif ($this->Openid->isOpenIDResponse()) {

        $response = $this->Openid->getResponse($returnTo);

        if ($response->status == Auth_OpenID_CANCEL) {
            $this->Session->setFlash("Google Login Cancelled");
            $this->redirect(array("controller" => "users", "action" => "login"));
        } elseif ($response->status == Auth_OpenID_FAILURE) {
            $this->Session->setFlash("Veficiation Failed: " . $response->message);
            $this->redirect(array("controller" => "users", "action" => "login"));
        } elseif ($response->status == Auth_OpenID_SUCCESS) {

            $axResponse = Auth_OpenID_AX_FetchResponse::fromSuccessResponse($response);
            debug ($response);
            debug ($axResponse);
            $this->Session->setFlash("Authenticated");
        }
    }