2017-09-14 2 views
0

J'essaie de travailler avec Google_Service_BigQueryDataTransfer. J'ai créé un compte de service et téléchargé le fichier json. J'ai défini GOOGLE_APPLICATION_CREDENTIALS avec le chemin complet pour le fichier json. Je crée GoogleClient et Google_Service_BigQueryDataTransferErreur Google_Service_BigQueryDataTransfer

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

j'obtenir la liste des sources

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

fonctionne correctement.

j'obtenir la liste des transferts

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

il fonctionne aussi correctement.

Je reçois des informations d'identification

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

ou

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

les deux demandes de retour null

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

et dernier je tente de créer le transfert

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

et ont erreur

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

J'ai eu cette erreur de la page

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

quand n'a pas accès à BigQuery. Maintenant, j'utilise le compte de service et peut lister dataSource et les transferts, mais ne peut pas créer de transfert de données.

Pouvez-vous dire, ce que je fais est faux?

Code de travail

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

après que j'autorise dans Google et donne accès. Je redirigent au 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() 
); 

et tous les travaux

Répondre

0

Les informations d'identification d'application par défaut ne sont pas suffisantes pour l'utilisation du service BugQuery TransferConfig (notez que si $response->hasValidCreds est true, les creds ne sont pas valides).

Veuillez prendre en compte Using OAuth 2.0 for Web Server Applications.

+0

Merci, d'accord, tout fonctionne. –

0

Code rigoureux.

  1. Vous devez créer un ID client OAuth avec un autre type.
  2. Télécharger json.
  3. obtenir le jeton d'actualisation.

    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. Créer un service avec google utilisation client REFRESH_TOKKEN de 3

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