context_error: The operation is not allowed for the given context
I am trying to create a new campaing using google ads api with php client library. And simply getting this error. I will post whole php code here since I don't really know what this error is about. The console outputs the following:
[2022-02-22T18:00:13.039301+03:00] google-ads.WARNING: Request made: Host: "googleads.googleapis.com", Method: "/google.ads.googleads.v10.services.CampaignBudgetService/MutateCampaignBudgets", CustomerId: 9445807394, RequestId: "ozBYeKzMcMLizUbsO0r8_w", IsFault: 1, FaultMessage: "["The operation is not allowed for the given context."]" [2022-02-22T18:00:13.042848+03:00] google-ads.NOTICE: Request ------- Method Name: /google.ads.googleads.v10.services.CampaignBudgetService/MutateCampaignBudgets Host: googleads.googleapis.com Headers: { "x-goog-api-client": "gl-php\/7.4.27 gccl\/14.0.0 gapic\/14.0.0 gax\/1.11.4 grpc\/1.43.0 rest\/1.11.4", "x-goog-request-params": "customer_id=9445807394", "developer-token": "REDACTED", "login-customer-id": "6165712462" } Request: {"customerId":"9445807394","operations":[{"create":{"name":"Test Budget #2022-02-22T18:00:12.342+03:00","amountMicros":"500000","deliveryMethod":"STANDARD"}}]} Response ------- Headers: { "request-id": "ozBYeKzMcMLizUbsO0r8_w", "date": "Tue, 22 Feb 2022 15:00:12 GMT", "alt-svc": "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000,h3-Q050=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\"" } Fault ------- Status code: 3 Details: Request contains an invalid argument. Failure: {"errors":[{"errorCode":{"contextError":"OPERATION_NOT_PERMITTED_FOR_CONTEXT"},"message":"The operation is not allowed for the given context.","location":{"fieldPathElements":[{"fieldName":"operations","index":0}]}}],"requestId":"ozBYeKzMcMLizUbsO0r8_w"} Request with ID 'ozBYeKzMcMLizUbsO0r8_w' has failed. Google Ads failure details: context_error: The operation is not allowed for the given context
The code is as follows:
<?php /** * Copyright 2018 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Ads\GoogleAds\Examples\BasicOperations; require __DIR__ . '/vendor/autoload.php'; use GetOpt\GetOpt; use Google\Ads\GoogleAds\Examples\Utils\ArgumentNames; use Google\Ads\GoogleAds\Examples\Utils\ArgumentParser; use Google\Ads\GoogleAds\Examples\Utils\Helper; use Google\Ads\GoogleAds\Lib\V10\GoogleAdsClient; use Google\Ads\GoogleAds\Lib\V10\GoogleAdsClientBuilder; use Google\Ads\GoogleAds\Lib\V10\GoogleAdsException; use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder; use Google\Ads\GoogleAds\V10\Common\ManualCpc; use Google\Ads\GoogleAds\V10\Enums\AdvertisingChannelTypeEnum\AdvertisingChannelType; use Google\Ads\GoogleAds\V10\Enums\BudgetDeliveryMethodEnum\BudgetDeliveryMethod; use Google\Ads\GoogleAds\V10\Enums\CampaignStatusEnum\CampaignStatus; use Google\Ads\GoogleAds\V10\Errors\GoogleAdsError; use Google\Ads\GoogleAds\V10\Resources\Campaign; use Google\Ads\GoogleAds\V10\Resources\Campaign\NetworkSettings; use Google\Ads\GoogleAds\V10\Resources\CampaignBudget; use Google\Ads\GoogleAds\V10\Services\CampaignBudgetOperation; use Google\Ads\GoogleAds\V10\Services\CampaignOperation; use Google\ApiCore\ApiException; /** This example adds new campaigns to an account. */ class AddCampaigns{ private const CUSTOMER_ID = '9445807394'; private const NUMBER_OF_CAMPAIGNS_TO_ADD = 2; public static function main(){ // Either pass the required parameters for this example on the command line, or insert them // into the constants above. $options = (new ArgumentParser())->parseCommandArguments([ ArgumentNames::CUSTOMER_ID => GetOpt::REQUIRED_ARGUMENT ]); // Generate a refreshable OAuth2 credential for authentication. $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build(); // Construct a Google Ads client configured from a properties file and the // OAuth2 credentials above. $googleAdsClient = (new GoogleAdsClientBuilder()) ->fromFile() ->withOAuth2Credential($oAuth2Credential) ->build(); try { self::runExample( $googleAdsClient, $options[ArgumentNames::CUSTOMER_ID] ?: self::CUSTOMER_ID ); } 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 ); } exit(1); } catch (ApiException $apiException) { printf( "ApiException was thrown with message '%s'.%s", $apiException->getMessage(), PHP_EOL ); exit(1); } } /** * Runs the example. * * @param GoogleAdsClient $googleAdsClient the Google Ads API client * @param int $customerId the customer ID */ public static function runExample(GoogleAdsClient $googleAdsClient, int $customerId){ // Creates a single shared budget to be used by the campaigns added below. $budgetResourceName = self::addCampaignBudget($googleAdsClient, $customerId); // Configures the campaign network options. $networkSettings = new NetworkSettings([ 'target_google_search' => true, 'target_search_network' => true, 'target_content_network' => false, 'target_partner_search_network' => false ]); $campaignOperations = []; for ($i = 0; $i < self::NUMBER_OF_CAMPAIGNS_TO_ADD; $i++) { // Creates a campaign. // [START add_campaigns_1] $campaign = new Campaign([ 'name' => 'Test #' . Helper::getPrintableDatetime(), 'advertising_channel_type' => AdvertisingChannelType::SEARCH, // Recommendation: Set the campaign to PAUSED when creating it to prevent // the ads from immediately serving. Set to ENABLED once you've added // targeting and the ads are ready to serve. 'status' => CampaignStatus::PAUSED, // Sets the bidding strategy and budget. 'manual_cpc' => new ManualCpc(), 'campaign_budget' => $budgetResourceName, // Adds the network settings configured above. 'network_settings' => $networkSettings, // Optional: Sets the start and end dates. 'start_date' => date('Ymd', strtotime('+1 day')), 'end_date' => date('Ymd', strtotime('+1 month')) ]); // [END add_campaigns_1] // Creates a campaign operation. $campaignOperation = new CampaignOperation(); $campaignOperation->setCreate($campaign); $campaignOperations[] = $campaignOperation; } // Issues a mutate request to add campaigns. $campaignServiceClient = $googleAdsClient->getCampaignServiceClient(); $response = $campaignServiceClient->mutateCampaigns($customerId, $campaignOperations); printf("Added %d campaigns:%s", $response->getResults()->count(), PHP_EOL); foreach ($response->getResults() as $addedCampaign) { /** @var Campaign $addedCampaign */ print "{$addedCampaign->getResourceName()}" . PHP_EOL; } } /** * Creates a new campaign budget in the specified client account. * * @param GoogleAdsClient $googleAdsClient the Google Ads API client * @param int $customerId the customer ID * @return string the resource name of the newly created budget */ // [START add_campaigns] private static function addCampaignBudget(GoogleAdsClient $googleAdsClient, int $customerId){ // Creates a campaign budget. $budget = new CampaignBudget([ 'name' => 'Test Budget #' . Helper::getPrintableDatetime(), 'delivery_method' => BudgetDeliveryMethod::STANDARD, 'amount_micros' => 500000 ]); // Creates a campaign budget operation. $campaignBudgetOperation = new CampaignBudgetOperation(); $campaignBudgetOperation->setCreate($budget); // Issues a mutate request. $campaignBudgetServiceClient = $googleAdsClient->getCampaignBudgetServiceClient(); $response = $campaignBudgetServiceClient->mutateCampaignBudgets( $customerId, [$campaignBudgetOperation] ); /** @var CampaignBudget $addedBudget */ $addedBudget = $response->getResults()[0]; printf("Added budget named '%s'%s", $addedBudget->getResourceName(), PHP_EOL); return $addedBudget->getResourceName(); } // [END add_campaigns] } AddCampaigns::main();
How to fix this error? Or what is it about?