【问题标题】:Bigquery could not get default credentialsBigquery 无法获取默认凭据
【发布时间】:2017-05-19 00:06:16
【问题描述】:

我正在尝试使用 Firebase 设置 Google Bigquery,但遇到了一些问题。我在我的机器(MacOS Sierra)上安装了gcloud,并在我的项目中通过作曲家安装了谷歌云。

我的项目中的以下代码:

# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';

# Imports the Google Cloud client library
use Google\Cloud\BigQuery\BigQueryClient;

# Your Google Cloud Platform project ID
$projectId = 'hidden here only';

# Instantiates a client
$bigquery = new BigQueryClient([
    'projectId' => $projectId
]);

# The name for the new dataset
$datasetName = 'test_dataset';

# Creates the new dataset
$dataset = $bigquery->createDataset($datasetName);

echo 'Dataset ' . $dataset->id() . ' created.';

我要做的只是通过库在 bigquery 中创建一个数据集,但由于以下错误,我无法做到:

Fatal error: Uncaught Google\Cloud\Exception\ServiceException: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information in /Applications/MAMP/htdocs/projects/work/bigquery-tests/vendor/google/cloud/src/RequestWrapper.php on line 219

我已尝试按照示例代码的说明运行gcloud beta auth applications-default login,但在登录浏览器后,错误仍然存​​在。任何帮助都会很棒,谢谢!

【问题讨论】:

    标签: php google-bigquery google-cloud-platform google-authentication google-authenticator


    【解决方案1】:

    您非常接近,只需要设置服务帐户默认凭据,请参见putenv 和useApplicationDefaultCredentials() 行。这是我使用库 https://github.com/google/google-api-php-client 的工作代码您需要从控制台获取您的服务帐户密钥文件:https://console.cloud.google.com/iam-admin/serviceaccounts/

    composer.json

    {
        "require": {
            "google/cloud": "^0.13.0",
            "google/apiclient": "^2.0"
        }
    }
    

    php 文件

    # Imports the Google Cloud client library
    use Google\Cloud\BigQuery\BigQueryClient;
    use Google\Cloud\ServiceBuilder;
    
    $query="SELECT repository_url, 
           repository_has_downloads 
    FROM   [publicdata:samples.github_timeline]
    LIMIT  10";
    $client = new Google_Client();
    putenv('GOOGLE_APPLICATION_CREDENTIALS='.dirname(__FILE__) . '/.ssh/dummyname-7f0004z148e1.json');//this can be created with other ENV mode server side
    $client->useApplicationDefaultCredentials();
    
    $builder = new ServiceBuilder([
                    'projectId' => 'edited',
            ]);
    
            $bigQuery = $builder->bigQuery();
    
            $job = $bigQuery->runQueryAsJob($query);
            $info=$job->info();
    //      print_r($info);
    //      exit;
            $queryResults = $job->queryResults();
    
            /*$queryResults = $bigQuery->runQuery(
                $query,
                ['useLegacySql' => true]);*/
    
            if ($queryResults->isComplete()) 
            {
                $i = 0;
                $rows = $queryResults->rows();
    
                foreach ($rows as $row) 
                {
                    $i++;
    
                    $result[$i] = $row;
                }
            } 
            else 
            {
                throw new Exception('The query failed to complete');
            }
    
            print_r($result);
    

    【讨论】:

    • 你不应该需要 google/apiclient 依赖来做到这一点。 putenv 调用应该足以实例化 ServiceBuilder。
    • @Pentium10 如何下载密钥文件?我有权查看密钥,但没有看到下载按钮。
    • @JoeScotto 请参阅以下链接以获取有关如何下载密钥文件的指南:googlecloudplatform.github.io/google-cloud-php/#/docs/v0.20.1/…
    • @davids 我想我已经成功了,我不再遇到连接错误。我现在有一个拒绝访问错误,我认为这至少意味着我已连接。
    猜你喜欢
    • 2018-02-20
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    • 2021-01-06
    相关资源
    最近更新 更多