2017-09-14 57 views
0

我嘗試使用Google_Service_BigQueryDataTransfer。 我創建了服務帳戶並下載了json文件。 我爲GOOGLE_APPLICATION_CREDENTIALS設置了json文件的完整路徑。 創建GoogleClient和Google_Service_BigQueryDataTransfer錯誤Google_Service_BigQueryDataTransfer

$client = new Google_Client(); 
     $client->useApplicationDefaultCredentials(); 
$client->setScopes(array(
       Google_Service_Bigquery::BIGQUERY 
      ) 
     ); 
$this->service = new Google_Service_BigQueryDataTransfer($client); 

我得到源

$this->service->projects_dataSources-> 
      listProjectsDataSources('projects/' . self::PROJECT_ID); 

的列表它工作正常。

我得到轉移

$this->service->projects_locations_transferConfigs-> 
     listProjectsLocationsTransferConfigs(
      'projects/' . self::PROJECT_ID . '/locations/eu' 
     ); 

它也可以正常工作的列表。

我拿到證書

$this->service->projects_locations_dataSources-> 
    checkValidCreds(   
    'projects/'.self::PROJECT_ID.'/locations/europe/dataSources/adwords', 
    new Google_Service_BigQueryDataTransfer_CheckValidCredsRequest() 
     ); 

$this->service->projects_dataSources->checkValidCreds(
      'projects/'.self::PROJECT_ID.'/dataSources/adwords', 
      new Google_Service_BigQueryDataTransfer_CheckValidCredsRequest() 
     ); 

兩個請求返回null

object(Google_Service_BigQueryDataTransfer_CheckValidCredsResponse)[174] 
    public 'hasValidCreds' => null 
........ 

和最後我嘗試創建傳輸

 $params = new Google_Service_BigQueryDataTransfer_TransferConfig(); 
     $params->setDestinationDatasetId('stat'); 
     $params->setDisplayName('ID' . $adword_id); 
     $params->setDataSourceId(self::DATA_SOURCE_ID); 
     $params->setDataRefreshWindowDays(10); 
     $params->setDisabled(false); 
     $params->setParams(['customer_id'=> (string)$adword_id]); 
     return $this->service->projects_locations_transferConfigs->create(
      $this->getParent(). '/locations/europe', 
      $params 
     ); 

,並有錯誤

Google_Service_Exception: { 
    "error": { 
    "code": 400, 
    "message": "Request contains an invalid argument.", 
    "errors": [ 
     { 
     "message": "Request contains an invalid argument.", 
     "domain": "global", 
     "reason": "badRequest" 
     } 
    ], 
    "status": "INVALID_ARGUMENT" 
    } 
} 

我從

https://cloud.google.com/bigquery/docs/reference/datatransfer/rest/v1/projects.transferConfigs/create 

有這個錯誤時,沒有存取BigQuery。 現在我使用服務帳戶,可以列出數據源和傳輸,但不能創建數據傳輸。

你能說,我做錯了嗎?

工作代碼

$this->client = new Google_Client(); 
$this->client->useApplicationDefaultCredentials(); 
$this->client->setAuthConfig('client_secret.json'); 
$this->client->setAccessType("offline"); 
$this->client->setIncludeGrantedScopes(true); 
$this->client->setScopes(array(
     Google_Service_Bigquery::BIGQUERY, 
     ADWORDS_SCOPE 
    ) 
); 
$this->client->setRedirectUri(self::REDIRECT_URI); 
$url = $this->getAuthUrl(); 
header('Location: ' . filter_var($url, FILTER_SANITIZE_URL)); 

後,我在谷歌授權並給予訪問。 我重定向到代碼

$this->client = new Google_Client(); 
$this->client->useApplicationDefaultCredentials(); 
$this->client->setAuthConfig(MCC_DIR . 'protected/config/client_secret.json'); 
$this->client->setAccessType("offline"); 
$this->client->setIncludeGrantedScopes(true); 
$this->client->setScopes(array(
     Google_Service_Bigquery::BIGQUERY, 
     ADWORDS_SCOPE 
    ) 
); 
$this->client->setRedirectUri(self::REDIRECT_URI); 
$code = $_GET['code']; 
$this->client->authenticate($code); 
$tokken = $this->client->getAccessToken(); 
$this->client->setAccessToken($tokken); 
$this->service = new Google_Service_BigQueryDataTransfer($this->client); 
$this->service->projects_locations_dataSources->checkValidCreds(
    'projects/1069829667403/locations/europe/dataSources/adwords', 
    new Google_Service_BigQueryDataTransfer_CheckValidCredsRequest() 
); 

和一切工作

回答

0

默認的應用程序憑據不足,無法使用BugQuery TransferConfig服務(請注意,除非$response->hasValidCredstrue,vim的信任狀無效)。

請考慮Using OAuth 2.0 for Web Server Applications

+0

謝謝,沒事,所有的作品。 –

0

Rigth代碼。

  1. 您需要創建其他類型的OAuth客戶端ID。
  2. 下載json。
  3. get refresh tokken。

    const BQ_API_SCOPE = Google_Service_Bigquery :: BIGQUERY。' 」 .Google_Service_Bigquery :: CLOUD_PLATFORM; const REDIRECT_URI ='urn:ietf:wg:oauth:2.0:oob'; ....

    $scopes = self::BQ_API_SCOPE; 
    $oauth2 = new OAuth2([ 
         'authorizationUri' => self::AUTHORIZATION_URI, 
         'redirectUri' => self::REDIRECT_URI, 
         'tokenCredentialUri' => CredentialsLoader::TOKEN_CREDENTIAL_URI, 
         'clientId' => $clientId, 
         'clientSecret' => $clientSecret, 
         'scope' => $scopes 
        ]); 
    
    echo $oauth2->buildFullAuthorizationUri(); 
    
    $stdin = fopen('php://stdin', 'r'); 
    print'After approving the application, enter the authorization code here: '; 
    $code = trim(fgets($stdin)); 
    fclose($stdin); 
    print "\n";   
    $oauth2->setCode($code); 
    $authToken = $oauth2->fetchAuthToken();   
    printf("Your refresh token is: %s\n\n", $authToken['refresh_token']); 
    
  4. 從3

    $客戶端=新Google_Client谷歌客戶端使用REFRESH_TOKKEN創建服務(); $ client-> setAuthConfig('client_secret.json'); $ client-> setAccessType(「offline」); $ client-> setIncludeGrantedScopes(true); $客戶端 - > setScopes(陣列( Google_Service_Bigquery :: BIGQUERY, Google_Service_Bigquery :: CLOUD_PLATFORM, Google_Service_Bigquery :: CLOUD_PLATFORM_READ_ONLY ) ); (file_exists(self :: ACCESS_TOKKEN_FILE)){tokken = file_get_contents(self :: ACCESS_TOKKEN_FILE); $ client-> setAccessToken($ tokken); ($ client-> isAccessTokenExpired()){tokken = $ client-> fetchAccessTokenWithRefreshToken(REFRESH_TOKKEN); file_put_contents(self :: ACCESS_TOKKEN_FILE,json_encode($ tokken)); } } else { $ tokken = $ client-> fetchAccessTokenWithRefreshToken(REFRESH_TOKKEN); file_put_contents(self :: ACCESS_TOKKEN_FILE,json_encode($ tokken)); } $ this-> service = new Google_Service_BigQueryDataTransfer($ client);