/**
  * @param string $key_location
  * @param string $email_address
  */
 function __construct($key_location, $email_address)
 {
     $this->email_address = $email_address;
     $this->key_file_location = $key_location;
     $this->client = new Google_Client();
     $key = file_get_contents($this->key_file_location);
     $cred = new Google_Auth_AssertionCredentials($this->email_address, Google_Service_Bigquery::BIGQUERY, $key);
     $this->client->setAssertionCredentials($cred);
     //setup proxy if neccesary
     /*$io = new Google_IO_Curl($this->client);
     		$curlOptions = array();
     		$curlOptions[CURLOPT_PROXY] = "http://proxy.local";
     		$curlOptions[CURLOPT_PROXYPORT] = "8080";
     		$io->setOptions($curlOptions);
     		$this->client->setIo($io);*/
     if ($this->client->getAuth()->isAccessTokenExpired() || $this->client->getAccessToken() == NULL || $this->client->getAccessToken() == '') {
         $auth = new Google_Auth_OAuth2($this->client);
         $auth->refreshTokenWithAssertion($cred);
         $token = $auth->getAccessToken();
         $this->client->setAccessToken($token);
     }
     // Instantiate a new BigQuery Client
     $this->bigqueryService = new Google_Service_Bigquery($this->client);
 }
 * - Refresh of an existing access token
 * - Storage of access tokens in sessions
 *
 * Requirements:
 *   - Google APIs Client Libraries: https://developers.google.com/discovery/libraries
 */
$client = new Google_Client();
$oauthClient = new Google_Auth_OAuth2($client);
$client->setApplicationName(SERVICE_APP_NAME);
$client->setClientId(SERVICE_CLIENT_ID);
$token_name = "oauth_client_" . $client_id . "_token";
$token = (array) json_decode($_SESSION[$token_name]);
if (isset($token["access_token"]) && time() <= $token["created"] + $token["expires_in"]) {
    $oauthClient->setAccessToken($_SESSION[$token_name]);
} else {
    $oauthClient->refreshTokenWithAssertion(new Google_Auth_AssertionCredentials(SERVICE_ACCOUNT_EMAIL, array(SCOPE), file_get_contents(KEY_FILE)));
    $_SESSION[$token_name] = $oauthClient->getAccessToken();
}
if ($oauthClient->getAccessToken()) {
    // All good, we've got a valid access token!
    echo 'All good, let\'s go!';
    // Set up some variables
    $asset_id = '09372590152434720789-16691395374091854705';
    $token = $oauthClient->getAccessToken();
    print $token;
    $url = 'https://www.googleapis.com/mapsengine/v1/tables/' . $asset_id . '/features?version=published&key=' . $token;
    print $url;
    $merge_on = 'features';
    // Call a request
    gme_paging($oauthClient, $url, array(), $merge_on);
} else {