Posts tagged with php

I am developing an application which integrates with Google Ads and syncing Ad/Campaign etc. data into my servers. I am getting authorization related error when I try to request some data from Google Ads API. Here is the steps I have done by now:

  • Applied to Google in terms of validating an OAuth application and scopes (Done, we got verification from Google and can ask for AdWords scope)
  • Applied to Google Ads for getting a developer token and got it. (Basic Access)
  • We are able to connect test accounts and successfully getting the response. But when we try it with real accounts we get the error below.

The code is also from the original Google Ads API example. I have tried with tons of different accounts but none of them seems to be working. When I try to obtain those data with same parameters from AdWords API instead of Google Ads API, it works. But Google AdWords PHP SDK is no longer maintained so I have to keep trying with Google Ads API. I share my code below:

{     $this->customerId = $customerId;     $this->clientId = $clientId;     $this->clientSecret = $clientSecret;     $this->accessToken = $accessToken;     $oAuth2Credential = (new OAuth2TokenBuilder())         ->withClientId($this->clientId)         ->withClientSecret($this->clientSecret)         ->withRefreshToken($this->accessToken)         ->build();     $this->googleAdsClient = (new GoogleAdsClientBuilder())         ->withOAuth2Credential($oAuth2Credential)         ->withDeveloperToken(env("GOOGLE_DEVELOPER_TOKEN"))         ->withLoginCustomerId((int) $this->customerId)         ->build(); } public function getCampaigns(): array{     try {         $campaigns = [];         $services = $this->googleAdsClient->getGoogleAdsServiceClient();         $results = $services->search($this->customerId, $this->campaignQuery(), ['pageSize' => self::PAGE_SIZE]);         foreach ($results->iterateAllElements() as $row) {             $campaigns[] = (new Campaign())->transform($row);         }         return $campaigns;     } catch (GoogleAdsException $googleAdsException) {         // TODO add error log         return [];     } }``` The error: ```Google\ApiCore\ApiException: {     "message": "The caller does not have permission",     "code": 7,     "status": "PERMISSION_DENIED",     "details": [         {             "@type": 0,             "data": "type.googleapis.com\/google.ads.googleads.v6.errors.GoogleAdsFailure"         },         {             "@type": 0,             "data": [                 {                     "errorCode": {                         "authorizationError": "DEVELOPER_TOKEN_PROHIBITED"                     },                     "message": "Developer token is not allowed with project '691752477594'."                 }             ]         }     ]}```   

I'm getting this error when running this code:

$googleAdsClient->getGoogleAdsServiceClient()->search($customerId, $query, $page_size); 

ERROR message: proto descriptor was previously loaded (included in multiple metadata bundles?): google/ads/googleads/v5/enums/policy_topic_evidence_destination_not_working_dns_error_type.proto

$googleAdsClient is an instance of \Google\Ads\GoogleAds\Lib\V5\GoogleAdsClient.

The getGoogleAdsServiceClient method comes from the ServiceClientFactoryTrait trait /Google/Ads/GoogleAds/Lib/V5/ServiceClientFactoryTrait.php

I'm running:

php 7.4.1 grpc module version => 1.34.0 protobuf version => 3.14.0 

Additional libs installed via composer:

google/grpc-gcp                       0.1.5      gRPC GCP library for channel management google/protobuf                       v3.13.0.1  proto library for PHP googleads/google-ads-php              v5.0.0     Google Ads API client for PHP grpc/grpc                             1.30.0     gRPC library for PHP 

I don't know enough about descriptors or the protobuf library to understand this error. It just started happening a couple days ago and I don't think we made any changes that would effect this. Any help/guidance would be much appreciated.

EDIT: forgot to link to SDK https://github.com/googleads/google-ads-php

EDIT: updated the following libs:

google/apiclient                     v2.9.1     Client library for Google APIs google/apiclient-services            v0.161.0   Client library for Google APIs google/auth                          v1.15.0    Google Auth Library for PHP google/common-protos                 1.3        Google API Common Protos for PHP google/gax                           1.7.0      Google API Core for PHP google/grpc-gcp                      0.1.5      gRPC GCP library for channel management google/protobuf                      v3.15.3    proto library for PHP googleads/google-ads-php             v7.0.0     Google Ads API client for PHP grpc/grpc                            1.35.0     gRPC library for PHP google/protobuf                      v3.15.3    proto library for PHP 

# sample return data from TargetingIdea following https://developers.google.com/adwords/api/docs/samples/php/optimization#get-keywords-related-to-a-seed-keyword Google\AdsApi\AdWords\v201809\o\TargetingIdea^ {#2396   #data: array:5 [     0 => Google\AdsApi\AdWords\v201809\o\Type_AttributeMapEntry^ {#2441       #key: "KEYWORD_TEXT"       #value: Google\AdsApi\AdWords\v201809\o\StringAttribute^ {#2439         #value: "digital agency thailand ff9d9077"         #AttributeType: "StringAttribute"         -parameterMap: array:1 [           "Attribute.Type" => "AttributeType"         ]       }     } 

The method of calling for the keyword idea generations are done correctly

        $querySearchParam = new RelatedToQuerySearchParameter();         $querySearchParam->setQueries($keywords);         $searchParameters[] = $querySearchParam;         $relatedUrlSearchParam = new RelatedToUrlSearchParameter();         $relatedUrlSearchParam->setUrls($urls);         $searchParameters[] = $relatedUrlSearchParam;         $languageParam = new LanguageSearchParameter();         $languageParam->setLanguages([new Language(1000)]);         $searchParameters[] = $languageParam;         $networkSearchParam = new NetworkSearchParameter();          $networkSearchParam->setNetworkSetting(new NetworkSetting(true));         $searchParameters[] = $networkSearchParam; 

However it returns a randomly generated alphanumeric after the keyword text. Eg: ff9d9077 at the end of the KEYWORD_TEXT attribute digital agency thailand. Where the same keyword idea returns multiple times but with different randomly generated trailing alphanumeric text such as digital agency thailand 13fdc22, digital agency thailand c73761c etc

Anyone can advice what might went wrong on this?

I'm trying to create an AdGroupAd using an existing groupAd, and i'm getting a "segmentation fault" error message I've been at this for more than a day now, but the lack of details on the error just makes this insane to understand what's wrong

running on docker

Client library: v5.0.0

Google Ads API: v5.0

PHP 7.4.12 (cli) (built: Nov 5 2020 20:24:10) ( NTS )

Zend Engine v3.4.0,

ionCube PHP Loader + ionCube24 v10.4.4

Zend OPcache v7.4.12

protobuff 3.14.0

grpc 1.33.1

my code: Assume createAdGroupAd() is called with a valid resource name for an adGroup in the $adGroup parameter and the type is CreativeType_Asset::TYPE_YOUTUBE in this case (although i tried with both types and both give out the error)

public function createAdGroupAd($adGroup, $data){         $service = $this->client->getAdGroupAdServiceClient();         $ad = $this->createAd($data, $data['asset']->type);         $adGroupAd = new AdGroupAd([             'ad' => $ad,             'status' => AdGroupAdStatus::PAUSED,             'ad_group' =>  $adGroup         ]);         $adGroupAdOperation = new AdGroupAdOperation();         $adGroupAdOperation->setCreate($adGroupAd);         //this is the call causing the error         $response = $service->mutateAdGroupAds(             $this->customerId,             [$adGroupAdOperation]         );         return $response->getResults()->count() > 0 ? $response->getResults()[0]->getResourceName() : null;     }     public function createAd($data, $adType = null){         $service = $this->client->getAdServiceClient();         $appAdInfo = new AppAdInfo([             'headlines' => array_map(function ($headline) {                 new AdTextAsset(['text' => $headline]);             }, [$data['creative']->title1, $data['creative']->title2]),             'descriptions' => array_map(function ($description) {                 new AdTextAsset(['text' => $description]);             }, [$data['creative']->description1, $data['creative']->description2])         ]);         switch ($adType) {             case CreativeType_Asset::TYPE_IMAGE:                 $appAdInfo->setImages([new AdImageAsset(['asset' => $data['assetResourceName']])]);                 break;             case CreativeType_Asset::TYPE_YOUTUBE:                 $appAdInfo->setYoutubeVideos([new AdVideoAsset(['asset' => $data['assetResourceName']])]);                 break;             default:                 break;         }         return new Ad([             'app_ad' => $appAdInfo,             'type' => AdType::APP_AD]);     } 

I am trying to do the pagination with google-ads-php at the bottom of my page in php, so I get through my ads, like PREVIOUS 1,2,3 NEXT So this is my code:

    public function runExample(GoogleAdsClient $googleAdsClient, int $customerId){         $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();         // Creates a query that retrieves all ads.         $query = "SELECT campaign.name, ad_group.name, "             . "ad_group_ad.ad.responsive_display_ad.marketing_images, "             . "ad_group_ad.ad.app_ad.images, ad_group_ad.ad.app_ad.youtube_videos, "             . "ad_group_ad.ad.responsive_display_ad.youtube_videos, ad_group_ad.ad.local_ad.videos, "             . "ad_group_ad.ad.video_responsive_ad.videos, ad_group_ad.ad.video_ad.media_file,  "             . "ad_group_ad.ad.app_engagement_ad.images, ad_group_ad.ad.app_engagement_ad.videos, "             . "ad_group_ad.ad.display_upload_ad.media_bundle, ad_group_ad.ad.gmail_ad.product_images, "             . "ad_group_ad.ad.gmail_ad.product_videos, ad_group_ad.ad.gmail_ad.teaser.logo_image, "             . "ad_group_ad.ad.image_ad.image_url, ad_group_ad.ad.legacy_responsive_display_ad.square_marketing_image, "             . "ad_group_ad.ad.local_ad.marketing_images, ad_group_ad.ad.responsive_display_ad.logo_images, "             . "ad_group_ad.ad.responsive_display_ad.square_logo_images, "             . "ad_group_ad.ad.responsive_display_ad.square_marketing_images, "             . "ad_group_ad.ad.responsive_display_ad.youtube_videos, "             . "metrics.impressions, campaign.campaign_budget, campaign.status, "             . "campaign.start_date, campaign.end_date, metrics.all_conversions, "             . "metrics.average_cost, ad_group_ad.ad.type, ad_group_ad.ad.id, "             . "campaign.campaign_budget, metrics.cost_micros, ad_group_ad.status, metrics.impressions "             . "FROM ad_group_ad "             . "WHERE segments.date >= '{$this->from}' AND segments.date <= '{$this->to}' "             . "ORDER BY campaign.name ASC";         // Issues a search stream request.         /** @var GoogleAdsServerStreamDecorator $stream */         $stream = $googleAdsServiceClient->search($customerId, $query, ['pageSize' => 10]);         $ads = [];         foreach ($stream->iterateAllElements() as $googleAdsRow) {             dump($googleAdsRow->serializeToJsonString());             /** @var GoogleAdsRow $googleAdsRow */             $ads[] = json_decode($googleAdsRow->serializeToJsonString(), true);         } 

As you see the pageSize is set to 10, so it will be 23 pages, because I have 230 ads.

How can I do the pagination, now the $stream returns all ads in one response. How can return only 10 ads, and then when user click for example second page button, it will return the next 10 ads, and so on?

Thanks in advance!