Posts tagged with google-cloud-iam

I'm attempting to use the Google Ads API Client Library for PHP with a Service Account json.

Inviting the Service Account to Google Ads account has status of "Awaiting response" - but I cannot login to the Service Account to accept the invite.

Here is the code being used:

<?php // Include the Google Ads API PHP client library require_once 'googleads_google-ads-php/vendor/autoload.php'; use Google\Ads\GoogleAds\Lib\V13\GoogleAdsClientBuilder; use Google\Ads\GoogleAds\Lib\V13\GoogleAdsServerStreamDecorator; use Google\Ads\GoogleAds\Util\V13\ResourceNames; use Google\Ads\GoogleAds\V13\Services\GoogleAdsQuery; use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder; $developerToken = "developerToken"; $jsonKeyFilePath = "json_key.json"; $customerId = "CustomerId"; // Set up authentication using a service account JSON file $googleAdsClient = (new GoogleAdsClientBuilder())     ->withDeveloperToken($developerToken) ->withOAuth2Credential((new OAuth2TokenBuilder())     ->withJsonKeyFilePath($jsonKeyFilePath)     ->withScopes(['https://www.googleapis.com/auth/adwords'])     ->build())     ->build(); // Define the query to retrieve data from the age_range_view $query = "SELECT segments.date, segments.device, metrics.clicks, ad_group_criterion.criterion_id, ad_group_criterion.display_name           FROM age_range_view           LIMIT 1000"; try {     // Execute the query and get the results     $response = $googleAdsClient->getGoogleAdsServiceClient()->search($customerId, $query);     // Loop through the rows of data and extract the desired values     foreach ($response->iterateAllElements() as $googleAdsRow) {         $date = $googleAdsRow->getSegments()->getDate()->getValue();         $device = $googleAdsRow->getSegments()->getDevice()->getValue();         $clicks = $googleAdsRow->getMetrics()->getClicks()->getValue();         $criterionId = $googleAdsRow->getAdGroupCriterion()->getCriterionId()->getValue();         $displayName = $googleAdsRow->getAdGroupCriterion()->getDisplayName()->getValue();                  // Print the extracted values to the console         printf("Date: %s | Device: %s | Clicks: %d | Criterion ID: %d | Display Name: %s\n",             $date,             $device,             $clicks,             $criterionId,             $displayName         );     } } catch (GoogleAdsException $googleAdsException) {     printf("Request with ID '%s' has failed.%sGoogle Ads failure details:%s",         $googleAdsException->getRequestId(),         PHP_EOL,         PHP_EOL     );     foreach ($googleAdsException->getGoogleAdsFailure()->getErrors() as $error) {         /** @var GoogleAdsError $error */         printf("\t%s: %s%s",             $error->getErrorCode()->getErrorCode(),             $error->getMessage(),             PHP_EOL         );     } } catch (ApiException $apiException) {     printf("ApiException was thrown with message '%s'.%s",         $apiException->getMessage(),         PHP_EOL     ); } ?> 

Here is the error message:

PHP Fatal error:  Uncaught Google\ApiCore\ApiException: {     "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https:\/\/developers.google.com\/identity\/sign-in\/web\/devconsole-project.",     "code": 16,     "status": "UNAUTHENTICATED",     "details": [         {             "@type": "type.googleapis.com\/google.ads.googleads.v13.errors.GoogleAdsFailure",             "errors": [                 {                     "errorCode": {                         "authenticationError": "NOT_ADS_USER"                     },                     "message": "User in the cookie is not a valid Ads user."                 }             ],             "requestId": "requestId"         }     ] }   thrown in /data_import/googleads_google-ads-php/vendor/google/gax/src/ApiException.php on line 267 

I attempted to try adding ->withImpersonatedEmail($impersonatedEmail) to the authentication request with an account on the same project that has access to the Google Ads accounts but that did not work.

What am I missing?

Note - Below all these points I am doing manually, I want to make it automatically with programming.

I have several questions regarding "google-cloud-iam"

Are these below points are possible programmatically via "google-cloud-iam" any type of API?

  1. Do Link Google Adwords account with Google analytics (User is using the same Gmail account for AdWords and Analytics account)
  2. Create a project in the cloud google console, enable "Analytics API", create a service account, and download the P12 Key at the server-side or client-side.
  3. Add Service account id in Google Analytics user management