4

有趣的是,我在两个不同的服务器(a.com,b.com)上使用相同的 .php 脚本,结果不同,我猜它们有不同的配置。虽然在 a.com 上我能够很好地完成 SSO 流程,但 b.com 会抛出“在https://www.google.com/accounts/o8/id上找不到 OpenID 服务器”。

我的 php 脚本如下所示:

$openid = new LightOpenID($_SERVER["HTTP_HOST"]);
$openid->required = array
(
    'contact/email',
    'namePerson/first',
    'namePerson/last'
);
if(!$openid->mode)
{
    $openid->identity = 'https://www.google.com/accounts/o8/id';
    header('Location: ' . $openid->authUrl());
}

在 b.com 中, $openid->authUrl() 行会抛出错误消息: No OpenID Server found at https://www.google.com/accounts/o8/id

什么服务器配置可能导致此问题?

4

2 回答 2

3

幸运的是,服务器管理员能够快速发现 php 配置中的配置差异allow_url_fopen = 1 解决了这个问题

于 2012-11-26T12:02:53.073 回答
3

Google 已停止对 OpenID 的支持,并于 2015 年 7 月 22 日删除了此端点。

批准的解决方案/答案已过时。

请迁移到 OAuth。如果使用客户端(javascript)登录,google 的本教程附带了一个功能齐全的示例。

下面的代码是另一个功能齐全的示例(刚刚测试过),但它更进一步并与服务器端 PHP 脚本集成。请替换您自己的客户 ID(在您的google 开发者控制台中定义)和 YOURDOMAIN.COM(在 php 中)。

<html lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <head>
    <meta name="google-signin-scope" content="profile email">
    <meta name="google-signin-client_id" content="1111111111111111111-11111111111111111111111111111111111.apps.googleusercontent.com">
    <script src="https://apis.google.com/js/platform.js" async defer></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">
    </script>
    <script>
      function signOut() {
        var auth2 = gapi.auth2.getAuthInstance();
        auth2.signOut().then(function () {
            console.log('User signed out.');
            userDataClear();
        });
      }
      function disassociate() {
        var auth2 = gapi.auth2.getAuthInstance();
        auth2.disconnect().then(function () {
            console.log('User disconnected from association with app.');
            userDataClear();
        });
      }
      function onSignIn(googleUser) {
            // Useful data for your client-side scripts:
            var profile = googleUser.getBasicProfile();
            console.log("ID: " + profile.getId()); // Don't send this directly to your server!
            // The ID token you need to pass to your backend:
            var id_token = googleUser.getAuthResponse().id_token;
            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'http://YOURDOMAIN.COM/twoStep02.php');
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhr.onload = function() {
                console.log('NEW Signed in Google Client ID: ' + xhr.responseText);
            };
        xhr.send('idtoken=' + id_token);
            console.log("ID Token: " + id_token);
            userDataDisplay(profile);
      }
      function userDataDisplay(profile) {
        document.getElementById("foto").innerHTML = '<img src="' + profile.getImageUrl() + '"></img>';
        document.getElementById("email").innerHTML = profile.getEmail();
        document.getElementById("nome").innerHTML = profile.getName();
      }
      function userDataClear() {
        document.getElementById("foto").innerHTML = ' ';
        document.getElementById("email").innerHTML = ' ';
        document.getElementById("nome").innerHTML = ' ';
      }
   </script>
  </head>
  <body>
    <div id="login-button" class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>
    <div><a href="/#" onclick="signOut();">Sign out</a></div>
    <div><a href="/#" onclick="disassociate();">Disassociate App and Site (easily undone)</a></div>
    <div id="foto"></div>
    <div id="nome"></div>
    <div id="email"></div>
  </body>
</html>

这是 jquery/ajax 调用的 php 端(twoStep.php)

<?php

    $idToken = $_POST["idtoken"];
    $url = 'https://www.googleapis.com/oauth2/v3/tokeninfo?id_token='.$idToken;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    $json = json_decode($response, true);
    curl_close($ch);

    $userEmail = $json["email"];
    $clientId = $json["azp"];
    print_r($json); // returns array console readable

?> 

提示 4 新手:通过右键单击页面的任何元素并选择“检查元素”来读取控制台输出,然后切换到 CONSOLE 选项卡。

于 2015-07-22T13:37:04.427 回答