错误 Google_Service_BigQueryDataTransfer

Error Google_Service_BigQueryDataTransfer

我尝试与 Google_Service_BigQueryDataTransfer 一起工作。 我创建了服务帐户并下载了 json 文件。 我将 GOOGLE_APPLICATION_CREDENTIALS 设置为 json 文件的完整路径。 我创建了 GoogleClient 和 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()
        );

两个请求return 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));

我在 google 中授权并授予访问权限后。 我重定向到代码

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

及全部作品

默认应用程序凭据不足以使用 BugQuery TransferConfig 服务(请注意,除非 $response->hasValidCredstrue,否则凭据无效)。

请考虑Using OAuth 2.0 for Web Server Applications

正确的代码。

  1. 您需要创建其他类型的 OAuth 客户端 ID。
  2. 下载 json 个凭据。
  3. 获取刷新令牌。
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']);     
  1. 使用 google 客户端使用 REFRESH_TOKKEN 从 3
  2. 创建服务
$client = new Google_Client();
$client->setAuthConfig('client_secret.json');
$client->setAccessType('offline');
$client->setIncludeGrantedScopes(true);
$client->setScopes(array(
        Google_Service_Bigquery::BIGQUERY,
        Google_Service_Bigquery::CLOUD_PLATFORM,
        Google_Service_Bigquery::CLOUD_PLATFORM_READ_ONLY
    )
);
if (file_exists(self::ACCESS_TOKKEN_FILE)){
    $tokken = file_get_contents(self::ACCESS_TOKKEN_FILE);
    $client->setAccessToken($tokken);
    if ($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);