2016-11-03 4 views
2

BigQuery에서 PHP 코드의 쿼리를 실행할 수 있도록 Google BigQuery API를 구현하고 싶습니다.Google BigQuery API PHP 사용자 인증

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

가 그럼 난 자격 증명을 작성하려면이 명령을 실행 : 나는 다음 명령하여 Google 클라우드 SDK를 설치 한

composer require google/cloud 

둘째 :

우선 다음 명령으로 클라이언트 라이브러리를 설치 한 :

gcloud beta auth application-default login 

모든 프로세스가 성공한 후 실행됩니다. 불행하게도 나는 메시지 오류 다음있어

# 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.'; 

을 :하지만

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에이 코드를 실행하려면 : L 요청 나는 다음과 같은 메시지가

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