2016-11-03 13 views
2

BigQueryのPHPコードからクエリを実行できるようにGoogle BigQuery APIを実装したいと思います。Google BigQuery API PHP認証

curl https://sdk.cloud.google.com | bash 

は、その後、私は資格を作成するには、このコマンドを実行します。私は、次のコマンドでのGoogle Cloud SDKをインストールしている

composer require google/cloud 

第二:

まず私は、次のコマンドにより、クライアントライブラリをインストールしています:

gcloud beta auth application-default login 

すべてのプロセスは成功し、実行後の信用度リットル要求私は、次のメッセージが表示されます:

Credentials saved to file: 

[/home/some/my/dir/application_default_credentials.json] 

These credentials will be used by any library that requests 
Application Default Credentials. 

それから私は、PHP上でこのコードを実行したい:

# 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 = 'PROJECT ID'; 

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

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

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

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

しかし残念ながら、私は、メッセージのエラーを以下ました:

Fatal error: Uncaught exception 'Google\Cloud\Exception\ServiceException' with message 'Could not load the default credentials 

だから私の質問を私はどうしたらいいですか?

+0

あなたは低料金です。重要なので、あなたは、投票の下の投稿された回答の左側にあるチェックマークを使用して、受け入れられた回答に印を付ける必要があります。これはあなたの率を高めます。どのようにこのリンクを見て、この作品を参照してください:http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work#5235 – Pentium10

答えて

2

デフォルトの資格情報がGoogle APIs Client Library for PHP、バージョン2.0.0以降で提供されていただきありがとうございます。 、それらを使用useApplicationDefaultCredentials呼び出すには:次に

$client = new Google_Client(); 
$client->useApplicationDefaultCredentials(); 

を、次のようにAPIサービスにアクセスするための資格情報を使用します。

$client->setScopes(['https://www.googleapis.com/auth/books']); 
$service = new Google_Service_Books($client); 
$results = $service->volumes->listVolumes('Henry David Thoreau'); 

私はそれがはるかに多くのオプションを持っており、私たち与えたリンクをチェックアウト示唆していますservice accountsを使用することをおすすめします。

+0

あなたの返信のためのこんにちは@ Pentium10ありがとう、私は追加しようとしている$ client-> useApplicationDefaultCredentials();私はこのリターンを得ました:致命的なエラー: 'Google_Client'クラスが見つかりません? –

+0

正しいバージョンの作者 'composer require google/apiclient:^ 2.0'を実行してください。githubにはたくさんの例があります – Pentium10

関連する問題