1

내가 액세스 토큰 내 경우 FireStore DB에 REST 통화를 할 수 얻으려고과 REST API의 경우 FireStore 액세스 토큰을 가져옵니다. 나는 (지금 유일한 방법) PHP를 사용하고, 다음 단계를 수행 한 다음 중포 기지 콘솔에 서비스 계정을 만든구글-API-PHP는 lib 디렉토리

에 포함되어 있으며,이 코드 생성 :

putenv('GOOGLE_APPLICATION_CREDENTIALS='.__DIR__ .'/key.json'); 
$client = new Google_Client(); 
$client->useApplicationDefaultCredentials(); 
$client->addScope('https://www.googleapis.com/auth/datastore,https://www.googleapis.com/auth/cloud-platform'); 
$client->authorize(); 

나는 또한 교체를 나는 $ 클라이언트를 위해서 var_dump 때 D

$client->setAuthConfig(__DIR__ .'/key.json'); 

$client->useApplicationDefaultCredentials(); 

부분은 이제 토큰은 NULL 유지됩니다.

내 서비스 계정에 대한 access_token을 얻는 방법을 알 수 없습니다. 아무도 내가 어떻게 액세스 토큰을 얻을 수 있는지 생각하지 않습니까?

답변

0
  1. 여러 범위를 추가하려면 문자열 대신 배열을 사용해야합니다. 문자열은 하나의 단일 범위로만 취급됩니다.

  2. $client->authorize()은 지정된 범위에 대해 Google 서비스에 액세스 할 수있는 Client 인스턴스를 반환합니다. 따라서 $authorizedClient = $client->authorize();이 더 적절할 것입니다.

  3. 그냥 액세스 토큰을 검색 할 경우, 당신은 당신이 뭔가를 줄 것 $client->fetchAccessTokenWithAssertion()을 사용할 수 있습니다 여기에

Array 
(
    [access_token] => ya29.c.............. 
    [token_type] => Bearer 
    [expires_in] => 3600 
    [created] => 1511280489 
) 
내가 이것을 테스트하는 데 사용되는 완전한 스크립트입니다 :

<?php 

require __DIR__.'/vendor/autoload.php'; 

$client = new Google_Client(); 
$client->useApplicationDefaultCredentials(); 
$client->addScope([ 
    'https://www.googleapis.com/auth/datastore', 
    'https://www.googleapis.com/auth/cloud-platform' 
]); 
// $client->authorize(); 

print_r($client->fetchAccessTokenWithAssertion()); 
+0

정말 고마워요! 이것은 트릭을 만들었습니다. 이제는 잘 작동합니다. –