0

私はFirebaseでGoogle Bigqueryをセットアップしようとしていて、問題が発生しています。私のマシン(MacOS Sierra)にgcloudがインストールされており、私のプロジェクトで作曲家経由でグーグルクラウドがインストールされています。Bigqueryがデフォルトの資格情報を取得できませんでした

私のプロジェクトで、次のコード:

# 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を実行しようとしました。どんな助けも素晴らしいだろう、ありがとう!

答えて

3

サービスアカウントのデフォルトクレデンシャルを設定する必要があります。putenvuseApplicationDefaultCredentials()の行を参照してください。これは、コンソールから鍵ファイルを占め、私はあなたがあなたのサービスを取得する必要があり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); 
+1

これを行うには、 'google/apiclient'の依存関係は必要ありません。 'putenv'呼び出しは、' ServiceBuilder'をインスタンス化するのに十分でなければなりません。 – jdp

+0

@ Pentium10どのようにしてキーファイルをダウンロードできますか?キーを表示する権限がありますが、ダウンロードボタンは表示されません。 –

+0

@JoeScottoキーファイルをダウンロードする方法については、次のリンクをご覧ください。 https://googlecloudplatform.github.io/google-cloud-php/#/docs/v0.20.1/guides/authentication –

関連する問題