2

我正在通过 oAuth 2.0 进行身份验证,使用来自 App Engine 的 Google API PHP 客户端库,以便在 Maps Engine(现在称为 My Maps)中检索当前用户的图层列表。

我正在使用以下代码 test.php:

<?php
session_start();

require_once 'Google/Client.php';
require_once 'Google/Service/MapsEngine.php';

$client_id = 'zzzzzzzzzzzzzzzzzzzzzzzzzzz';
$client_secret = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];

$client = new Google_Client();
$client->setAccessType('online');
$client->setApplicationName('myappname');
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setDeveloperKey('rrrrrrrrrrrrrrrrrrrrrrrrrrrrr'); 
$client->setScopes("https://www.googleapis.com/auth/mapsengine");
$mymaps_service = new Google_Service_MapsEngine($client);

if (isset($_REQUEST['logout'])) 
{
    unset($_SESSION['access_token']);
}

if (isset($_GET['code']))
{
    $client->authenticate($_GET['code']);
    $_SESSION['access_token'] = $client->getAccessToken();
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

echo "<html><body>";

if (isset($_SESSION['access_token']) && $_SESSION['access_token'])
{
    $client->setAccessToken($_SESSION['access_token']);
    echo "<p>Access token set OK!</p>";
}
else
{
    $authUrl = $client->createAuthUrl();
    echo "<p>Could not authenticate.</p>";
    echo "<p><a href='$authUrl' target=_blank>Try to authenticate by following this URL</a></p>";
}

if ($client->getAccessToken())
{
    $_SESSION['access_token'] = $client->getAccessToken();
    echo "<h1>Your Maps Engine Layer List:</h1>";
    $layers = $mymaps_service->layers->listLayers(array());
    var_dump($layers);
}
else
{
    echo "<p>Could not get the Access Token.</p>";
}

echo "</body></html>";
?>

我已经创建了我的项目、客户端 ID、秘密、地图引擎的开发人员密钥(简单的 api 访问密钥)、地图引擎中的图层(它们是公共的),还设置了可以连接到我的应用程序的 IP应用程序引擎,以及 test.php 可以从中进行身份验证的 IP 范围。

当我部署 test.php 时,在浏览器中注销并加载 test.php 我立即收到消息:“无法进行身份验证。尝试通过以下 URL 进行身份验证无法获取访问令牌”,即没有浏览器显示 Google 的帐户选择页面。

然后我点击“尝试通过此 URL 进行身份验证”链接,它会打开 Google 的帐户选择页面。我提供了我知道在地图引擎中拥有图层的帐户的用户名和密码,然后我收到消息:“访问令牌设置正常!您的地图引擎图层列表:”

但是没有图层列表出现......好像 $layers 变量不会从 listLayers 方法中得到任何东西。

当我尝试使用developer.google.com上的演示获取列表时,我确实得到了层列表。

为了获取我的用户在 Maps Engine 中可以访问的图层列表,我可以在我的代码中修改哪些内容?

4

0 回答 0