Reputation: 2271
I have downloaded the google-api-php-client for accessing the google api. my code is
include_once "templates/base.php";
echo pageHeader("Simple API Access");
set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Bigquery.php';
$client = new Google_Client();
$client->setApplicationName("my application name");
$apiKey = "--------my api key----";
$client->setDeveloperKey($apiKey);
define("PROJECT_ID", my project ID);
define("DATASET_ID", "my data set ID");
$service = new Google_Service_Bigquery($client);
$results = $service->tables->listTables(PROJECT_ID, DATASET_ID);
print_r($results);
if i run the above file it givet the error :
Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/bigquery/v2/projects/projectID/datasets/datasetname/tables?key=API key: (401) Login Required' in C:\xampp\htdocs\test\google-api-php-client-master (2)\google-api-php-client-master\src\Google\Http\REST.php:80 Stack trace: #0 C:\xampp\htdocs\test\google-api-php-client-master (2)\google-api-php-client-master\src\Google\Http\REST.php(44): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request)) #1 C:\xampp\htdocs\test\google-api-php-client-master (2)\google-api-php-client-master\src\Google\Client.php(499): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request)) #2 C:\xampp\htdocs\test\google-api-php-client-master (2)\google-api-php-client-master\src\Google\Service\Resource.php(195): Google_Client->execute(Object(Google_Http_Request)) #3 C:\xampp\htdocs\test\google-api-php-client-master (2)\google-api-php-client-master\src\Google\Service\B in C:\xampp\htdocs\test\google-api-php-client-master (2)\google-api-php-client-master\src\Google\Http\REST.php on line 80
Upvotes: 1
Views: 1010
Reputation: 207830
You need to authentiate your app using OAUTH2.0
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name, array(
'https://www.googleapis.com/auth/bigquery',
), $key
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
var_dump($_SESSION);
$bq = new Google_Service_Bigquery($client);
Upvotes: 1