使用服务帐户通过PHP API访问BigQuery时出现“无效凭据”
问题描述:
I'm attempting to access Big Query through Google's php API using a service account.
<?php
require_once '../google-api-php-client/src/Google_Client.php';
require_once '../google-api-php-client/src/contrib/Google_BigqueryService.php';
define("CLIENT_ID", 'removed-removed.apps.googleusercontent.com');
define("SERVICE_ACCOUNT_NAME", 'removed-removed@developer.gserviceaccount.com');
define("KEY_FILE", '../../../key.p12');
define("PROJECT_ID", removed);
$client = new Google_Client();
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/prediction'),
$key)
);
$client->setClientId(CLIENT_ID);
// Instantiate a new BigQuery Client
$service = new Google_BigqueryService($client);
try
{
$service->tables->listTables(PROJECT_ID, "data");
} catch (Exception $e)
{
echo $e->getMessage();
}
?>
Error calling GET https://www.googleapis.com/bigquery/v2/projects/removed/datasets/data/tables: (401) Invalid Credentials
我正在尝试使用服务帐户通过Google的php API访问Big Query。 p> \ n
&lt;?php
require_once'../google-api-php-client/src/Google_Client.php';
require_once'../google-api-php-client/src/ contrib / Google_BigqueryService.php';
define(“CLIENT_ID”,“removed-removed.apps.googleusercontent.com”);
define(“SERVICE_ACCOUNT_NAME”,“removed-removed@developer.gserviceaccount.com”); \ ndefine(“KEY_FILE”,“.. / .. / .. / key.p12”);
nnnfine(“PROJECT_ID”,已移除);
$ client = new Google_Client();
$ key = file_get_contents(KEY_FILE);
$ client-&gt; setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/prediction'),
$ key)
);
$ client-&gt; setClientId(CLIENT_ID);
//实例化新的BigQuery Client
$ service = new Google_BigqueryService($ client);
try
{
$ service-&gt; tables-&gt; listTables(PROJ ECT_ID,“data”);
} catch(Exception $ e)
{
echo $ e-&gt; getMessage();
}
?&gt;
code> pre >
调用GET时出错
https://www.googleapis.com/bigquery/v2/projects/removed/datasets/data/tables :
(401)凭证无效 p>
blockquote>
DIV>
答
Make sure you are using the correct OAuth scope - the scope in the code above is for the Google Prediction API. Use https://www.googleapis.com/auth/bigquery
for full access to the BigQuery API. More about BigQuery API authorization here.
Example:
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/bigquery'),
$key)
);