【问题标题】:"Invalid Credentials" when accessing BigQuery through PHP API using service account使用服务帐户通过 PHP API 访问 BigQuery 时出现“无效凭据”
【发布时间】:2013-01-31 00:15:11
【问题描述】:

我正在尝试使用服务帐户通过 Google 的 php API 访问 Big Query。

<?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();
}

?>

调用 GET 时出错 https://www.googleapis.com/bigquery/v2/projects/removed/datasets/data/tables: (401) 无效的凭证

【问题讨论】:

    标签: php google-api google-bigquery google-api-php-client


    【解决方案1】:

    确保您使用的是正确的 OAuth 范围 - 上面代码中的范围适用于 Google Prediction API。使用 https://www.googleapis.com/auth/bigquery 获得对 BigQuery API 的完全访问权限。详细了解 BigQuery API authorization here

    例子:

    $client->setAssertionCredentials(new Google_AssertionCredentials(
                    SERVICE_ACCOUNT_NAME,
                    array('https://www.googleapis.com/auth/bigquery'),
                    $key)
    );
    

    【讨论】:

    • 就是这样。正在查看预测 API 的服务帐户教程。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 2021-09-05
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多