使用API PHP客户端库从App Engine使用oAuth 2.0获取Maps Engine用户图层列表

问题描述 投票:2回答:0

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

我正在使用以下代码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进行身份验证”链接,它会打开Goog​​le的帐户选择页面。 我为我拥有地图引擎中的图层的帐户提供用户名和密码,然后收到消息:“访问令牌设置正确!您的地图引擎图层列表:”

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

当我尝试通过developers.google.com上的演示获取列表时,我确实获得了图层列表。

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

oauth google-api-php-client google-maps-engine
© www.soinside.com 2019 - 2024. All rights reserved.