Search code examples
google-bigquerydata-transfer

Error Google_Service_BigQueryDataTransfer


I try to work with Google_Service_BigQueryDataTransfer. I created service account and download json file. I set GOOGLE_APPLICATION_CREDENTIALS with full path for json file. I create GoogleClient and Google_Service_BigQueryDataTransfer

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

I get list of source

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

it works correctly.

I get list of transfers

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

it also works correctly.

I get credentials

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

or

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

both requests return null

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

and last i try to create transfer

        $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
        );

and have error

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"
  }
}

I had this error from page

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

when hadn't access to bigquery. Now i use service account and can list dataSource and transfers, but can't create datatransfer.

Can you say, what i do is wrong?

work code

$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));

after i authorize in google and give access. I redirect to code

$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()
);

and all works


Solution

  • The default application credentials are insufficient for using BugQuery TransferConfig service (note that unless $response->hasValidCreds is true, the creds are invalid).

    Please consider Using OAuth 2.0 for Web Server Applications.