I am authenticating via oAuth 2.0, using Google's API PHP Client library from App Engine, in order to retrieve the current user's list of layers in Maps Engine (now called My Maps).
I'm using the following code 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>";
?>
I have created my project, the client id, the secret, the developer key (simple api access key) for maps engine, the layers in maps engine (they are public), have also set up the IPs that can connect to my app in app engine, and the IP range that test.php can authenticate from.
When I deploy test.php, am logged out in the browser and load test.php I immediately get the message: "Could not authenticate. Try to authenticate by following this URL Could not get the Access Token", that is, without the browser displaying Google's account select page.
Then I clic on the "Try to authenticate by following this URL" link and it opens up Google's account selection page. I provide my user and password for the account I know owns the layers in maps engine, and then I get the message: "Access token set OK! Your Map Engine Layer List:"
But no layer list appears... as if the $layers variable wouldn't get anything from the listLayers method.
When I try to get the list using the demo at developers.google.com I do get a list of layers OK.
What can I modify in my code in order to get the list of layers my user has access to in Maps Engine?