Posts tagged with google-api

I am trying to limit the number of returned keyword ideas. The code works fine, but when I try to set limit to the keywordIdeas that I get using 'pageSize' I get the following error:

An unexpected error occurred: { "message": "Resource has been exhausted (e.g. check quota).", "code": 8, "status": "RESOURCE_EXHAUSTED", "details": [ { "@type": "type.googleapis.com\/google.ads.googleads.v14.errors.GoogleAdsFailure", "errors": [ { "errorCode": { "quotaError": "RESOURCE_EXHAUSTED" }, "message": "Too many requests. Retry in 4 seconds.", "details": { "quotaErrorDetails": { "rateScope": "ACCOUNT", "rateName": "Requests per service per method", "retryDelay": "4s" } } } ], "requestId": "eTMFNxlHYYTzZGDT7Cksfw" } ] }

Which is odd, because I am trying to limit the results and without pageSize this is error is not displayed.

Here is the code:

namespace Google\Ads\GoogleAds\Examples\Planning; 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\Lib\OAuth2TokenBuilder; use Google\Ads\GoogleAds\Lib\V14\GoogleAdsClient; use Google\Ads\GoogleAds\Lib\V14\GoogleAdsClientBuilder; use Google\Ads\GoogleAds\Lib\V14\GoogleAdsException; use Google\Ads\GoogleAds\Util\V14\ResourceNames; use Google\Ads\GoogleAds\V14\Enums\KeywordPlanNetworkEnum\KeywordPlanNetwork; use Google\Ads\GoogleAds\V14\Errors\GoogleAdsError; use Google\Ads\GoogleAds\V14\Services\GenerateKeywordIdeaResult; use Google\Ads\GoogleAds\V14\Services\KeywordAndUrlSeed; use Google\Ads\GoogleAds\V14\Services\KeywordSeed; use Google\Ads\GoogleAds\V14\Services\UrlSeed; use Google\ApiCore\ApiException; function generateKeywordIdeas($customerId, $url, $language, $locationId){     try {         // Google Ads API Configurations        $config = [             'developerToken' => '***',             'clientId' => '***',             'clientSecret' => '***',             'refreshToken' => '***',             'loginCustomerId' => '***',              'useSandbox' => true, // Set to true if you want to use the sandbox environment for testing.             'useImplicitConversion' => false, // Set to true if you want to automatically convert currency and units.         ];                 $oAuth2Credential = (new OAuth2TokenBuilder())         ->fromFile($_SERVER['DOCUMENT_ROOT'] . '/google-ads-php/google_ads_php.ini')         ->build();         $googleAdsClient = (new GoogleAdsClientBuilder())         ->fromFile($_SERVER['DOCUMENT_ROOT'] . '/google-ads-php/google_ads_php.ini')         ->withOAuth2Credential($oAuth2Credential)         ->build();                  $pageUrl = null;         $keywords = ['marketing','seo','advertising'];                        $locationIds = [21167];         $geoTargetConstants =  array_map(function ($locationId) {             return ResourceNames::forGeoTargetConstant($locationId);         }, $locationIds);         $keywordPlanIdeaServiceClient = $googleAdsClient->getKeywordPlanIdeaServiceClient();               $requestOptionalArgs = [];         if (empty($keywords)) {             // Only page URL was specified, so use a UrlSeed.             $requestOptionalArgs['urlSeed'] = new UrlSeed(['url' => $pageUrl]);         } elseif (is_null($pageUrl)) {             // Only keywords were specified, so use a KeywordSeed.             $requestOptionalArgs['keywordSeed'] = new KeywordSeed(['keywords' => $keywords]);         } else {             // Both page URL and keywords were specified, so use a KeywordAndUrlSeed.             $requestOptionalArgs['keywordAndUrlSeed'] =                 new KeywordAndUrlSeed(['url' => $pageUrl, 'keywords' => $keywords]);         }         $response = $keywordPlanIdeaServiceClient->generateKeywordIdeas(             [                 // Set the language resource using the provided language ID.                 'language' => ResourceNames::forLanguageConstant(1000),                 'customerId' => ***,                 // Add the resource name of each location ID to the request.                 'geoTargetConstants' => $geoTargetConstants,                 'keywordPlanNetwork' => KeywordPlanNetwork::GOOGLE_SEARCH,                 'pageSize'=>50                            ]+$requestOptionalArgs         );         // Iterate over the results and print its detail.         foreach ($response->iterateAllElements() as $result) {         var_dump($result->getText());                 } catch (GoogleAdsException $e) {         printf(             "An error occurred while generating keyword ideas: %s\n",             $e->getMessage()         );     } catch (\Throwable $e) {         printf(             "An unexpected error occurred: %s\n",             $e->getMessage()         );     } } generateKeywordIdeas($customerId, $url, $language, $locationId); ?> 

I've made a script that runs on the Google Ads API. The project on console.cloud.google.com is configured as "External" and "In Testing". The script I'm talking about needs to be run daily on a cron job. Issue is, every week I have to get a new refresh token manually, which is unnecessary in my opinion. So, is there a way to get a new refresh token without needing a user pressing "Continue" and "Accept", i.e. verifying that they allow access to Google Ads? Or does that only happen when I Publish my app on console.cloud.google.com?

So far, I either put the refresh token into the script and it works or (in another script) I use the refresh token to cURL into https://oauth2.googleapis.com/token to get an access token.

I follow all step of connectivity to Google Ads API. Using it I get account info like account number and time zone etc. But when I call API for campaign id or ads group at that time it return below output. In ads account (ui) I can see many campaigns.

output :

SearchPager<field_mask { paths: "campaign.id" paths: "ad_group.id"paths: "ad_group.name"}>

additionally : above output is getting in result variable and after that the for loop is not executing in image ofimage of code code

I'm attempting to cURL the Google Ads API with the following PHP code:

$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://googleads.googleapis.com/v14/customers/[CUSTOMER_ID]:generateKeywordHistoricalMetrics'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_HTTPHEADER, [     'developer-token: [DEVELOPER_TOKEN',     'login-customer-id: [LOGIN_CUSTOMER_ID]',     'Authorization: Bearer [???????]',     'Accept: application/json',     'Content-Type: application/json', ]); curl_setopt($ch, CURLOPT_POSTFIELDS, '{"historicalMetricsOptions":{"yearMonthRange":{"start":{"month":"JUNE","year":2022},"end":{"month":"MAY","year":2023}}},"keywords":[""]}'); $response = curl_exec($ch); curl_close($ch); 

And the response is:

{   "error": {     "code": 401,     "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",     "status": "UNAUTHENTICATED"   } } 

What I believe the issue to be is on the "Authentication" field. So far, I've put the following in that field(and failed each time):

  1. OAUTH 2 Client ID
  2. OAUTH 2 Client Secret
  3. OAUTH 2 Refresh Token, that works for another Google Ads project I've made
  4. Developer token

So, I could appreciate any and all help, since I'm running out of ideas and don't know what to do.

I am creating a web platform on which we would like to let our users (many thousands) use the Google Keyword Planer tool via API to look up word statisks and also suggest new words, if possible.

From what I can read in Google's API docs you have to have an account with Google to use the KW planer. Isn't it possible to somehow use KW planer via API without have each user creating a Google Ads account? Paid or not paid.

Our new online service can of course have a hidden central Google account as long as we can provide volume searches as explained above. But it looks like Google slows down search speed if you do too many searches, and we can't have that.

I am happy for any suggestion on how to get access to the KW planer via API for large volume use. Semrush does something like this. They surely must get their data from Google.

Thanks for helping!