Page token has expired

1,265 views
Skip to first unread message

Lukas St

unread,
May 12, 2021, 3:31:42 AM5/12/21
to AdWords API and Google Ads API Forum
The error occurs when downloading several millions of keywords that take around 2 or 3 hours using the google ads PHP library. 
The error was already described on this GitHub repo https://github.com/googleads/google-ads-php/issues/366

However, a solution how to refresh the page token when paginating through the results was not provided, but that's exactly what my system would need.

Here is part of my code:
$gaql = "SELECT campaign.id, "
                . "campaign.name, "
                . "ad_group.id, "
                . "ad_group.name, "
                . "metrics.impressions, "
                . "metrics.clicks, "
                . "metrics.cost_micros "
                . "FROM keyword_view";
$stream = $this->search($gaql, ['pageSize' => 5000]);
while (true) {
$page = $stream->getPage();

foreach ($page->getIterator() as $googleAdsRow) {
// work with data
}

    $nextPageToken = $page->getNextPageToken();
    $stream        = $this->search($gaql, ['pageSize' => 5000, 'pageToken' =>    $nextPageToken]);

    if ($nextPageToken == false) {
        echo "[INFO] There are no more pages" . "\n";
        break;
    }
}

After running for an hour and more without problems:

{
"errors": [{
"errorCode": {
"requestError": "EXPIRED_PAGE_TOKEN"
},
"message": "Page token has expired."
}]
}

Lukas St

unread,
May 12, 2021, 6:46:39 AM5/12/21
to AdWords API and Google Ads API Forum
It seems like the page token expires after 120 minutes...

Google Ads API Forum Advisor Prod

unread,
May 12, 2021, 12:22:02 PM5/12/21
to lukasmast...@gmail.com, adwor...@googlegroups.com
Hi Lukas,

Thanks for reaching out. Would it be possible for you to retry this request with a smaller page_size? This will break up the results into several responses and will help the page token from expiring. 

If this is not possible, we can submit a feature request on your behalf for refreshing a page token. 

Regards,
Matt
Google Ads API Team

Google Logo
Matt
Google Ads API Team
 


ref:_00D1U1174p._5004Q2HAFqj:ref

Lukas St

unread,
May 17, 2021, 3:00:39 AM5/17/21
to AdWords API and Google Ads API Forum
Hey Matt,

I made a workaround where I first download all campaign ids into my database and then I use the ids in a query that retrieves the keywords.
This allows me to segment keywords into smaller chunks.
So the new feature won't be necessary and the team can work on more important tasks :).

Best,
Lukas.

Google Ads API Forum Advisor Prod

unread,
May 17, 2021, 9:49:04 AM5/17/21
to lukasmast...@gmail.com, adwor...@googlegroups.com
Hi Lukas,

Glad to hear you found a workaround. Please let us know if any related issues arise. 

Joey Sumi

unread,
Oct 26, 2021, 3:27:02 PM10/26/21
to AdWords API and Google Ads API Forum

I, too, am experiencing this issue. How long does it take before a page_token expires?

I can paginate through hundreds of times but after a few hours, I get that error response. I've tried querying with a page size of 1,000-10,000 and it doesn't matter. Eventually the next_page_token expires. Does the expiration date begin at the beginning of the query or each time the page_token is created? As I said before, it takes HOURS to pull in the data.

I've also tried "search_stream" but I eventually get a timeout error that way too.

Here is the log of the request and response:
Method: /google.ads.googleads.v8.services.GoogleAdsService/Search
Host: googleads.googleapis.com
Headers: {
"developer-token": "REDACTED",
"login-customer-id": "9439287549",
"x-goog-api-client": "gl-python/3.6.9 grpc/1.41.0 gax/1.31.1",
"x-goog-request-params": "customer_id=4562346636"
}
Request: customer_id: "4562346636"
query: "SELECT segments.date,customer.currency_code,customer.id,campaign.name,campaign.id,ad_group.name,ad_group.id,search_term_view.search_term,segments.search_term_match_type,segments.device,segments.ad_network_type,metrics.clicks,metrics.cost_micros,metrics.impressions,metrics.conversions,metrics.conversions_value,metrics.all_conversions,metrics.all_conversions_value\nFROM search_term_view\nWHERE segments.date BETWEEN \'2021-08-01\' AND \'2021-08-01\'"
page_token: "CMDG8c2b0LrdfBCwlb4DGKaK3M7KLyIWQXNXZjBsazBpTUt6QW5CMWkwVHNEZyoCVjgwADim4OytC0D___________8B"


Response
-------
Headers: {
"google.ads.googleads.v8.errors.googleadsfailure-bin": "\n\u001d\n\u0002\b\b\u0012\u0017Page token has expired.\u0012\u0016kjbtXoxu5148I1uQim0FrQ",
"grpc-status-details-bin": "\b\u0003\u0012%Request contains an invalid argument.\u001a~\nCtype.googleapis.com/google.ads.googleads.v8.errors.GoogleAdsFailure\u00127\n\u001d\n\u0002\b\b\u0012\u0017Page token has expired.\u0012\u0016kjbtXoxu5148I1uQim0FrQ",
"request-id": "kjbtXoxu5148I1uQim0FrQ"
}
Fault: errors {
error_code {
request_error: EXPIRED_PAGE_TOKEN

}
message: "Page token has expired."
}
request_id: "kjbtXoxu5148I1uQim0FrQ"

-Joey

Google Ads API Forum Advisor

unread,
Oct 27, 2021, 7:52:42 AM10/27/21
to js...@alightanalytics.com, adwor...@googlegroups.com

Hi Joey,

Thanks for posting your concern on this thread.

Kindly note that next_page_token is supposed to be valid for 2 hours. If you exceed 2 hours, you are most likely to get the "Page token has expired" error. However, if you get "Page token has expired" error within 2 hours, and logs proved that the running time for that page query was within 2 hours, then you may provide us complete logs directly to our googleadsa...@google.com alias instead, so that I can raise the issue to our rest of team.

In addition, since you already tried to use search_stream instead of search, and you faced a timeout error. This could be due to large volumes of data pulling. You may consider pulling smaller chunks so it will not result in timeout.

Let us know if you have any further questions.

Regards,

Google Logo
Yasar
Google Ads API Team
 


ref:_00D1U1174p._5004Q2HAFqj:ref

Joey Sumi

unread,
Oct 27, 2021, 3:12:15 PM10/27/21
to AdWords API and Google Ads API Forum
Hello Yasar,

Is the next_page_token valid for 2 hours after the initial query request or 2 hours after the pagination request?
I can try to re-run a query to see whether or not I'm getting it after 2 hours or if it takes longer.

I'm already trying to pull Search Query data for one day. Is there a way to filter the request to pull a portion of a day?

Google Ads API Forum Advisor

unread,
Oct 28, 2021, 5:13:23 AM10/28/21
to js...@alightanalytics.com, adwor...@googlegroups.com

Hello Joey,

Thanks for getting back to us. I work with Yasar and I'll be assisting you this time.

As per your question on the lifetime of the next_page_token, if you receive a page with 10,000 rows and a page token that expires in 2 hours (or 7,200 seconds), then that leaves you with 0.72 seconds to process each row. You can also refer to this forum thread as briefly discussed by Ben.

With regard to filtering the request to pull a portion of a day, I'm afraid that this isn't possible.

Regards,

Google Logo
Mark Kevin Albios
Google Ads API Team
 


ref:_00D1U1174p._5004Q2HAFqj:ref
Message has been deleted

Google Ads API Forum Advisor

unread,
Oct 28, 2021, 10:04:54 PM10/28/21
to js...@alightanalytics.com, adwor...@googlegroups.com
Hi Joey,

Thank you for providing further details to your concern. I am also a member of the Google Ads API team and let me provide support to your concern.

I would like to inform you first that I am going to delete your current post as it contains your personal information.

Moving forward, could you try to removing the page_token from the initial results? I asked this because the value for page_token should only be set for the succeeding request from the initial, to request for the next page of results. For succeeding batches, use the value obtained from next_page_token in the latest response in order to request the next page of results.

If the error persists, please provide the updated request and response logs with request ID and request header generated on your end with the error so that I can further investigate. You can provide it via Reply privately to author option. If this option is not available, then send it instead on this email address googleadsa...@google.com.

Regards,
Google Logo
Ernie John Blanca Tacata
Google Ads API Team
 


ref:_00D1U1174p._5004Q2HAFqj:ref
Reply all
Reply to author
Forward
0 new messages