OAuth2 token errors with message: “error” : “invalid_grant”

Last Updated on April 30, 2017 by Chris Christoff

This documentation page refers to Google Analytics Dashboard for WP (GADWP) plugin.

For other FAQ, tutorials, and helpful resources see the GADWP documentation index.

The last Google Analytics API error generated by the plugin is displayed under Google Analytics -> Errors & Debug screen. If something goes wrong, you should check this screen for the last error code and description.

In addition to the solution presented below, a full list of common error codes is available here.

Error refreshing the OAuth2 token, message: {“error” : “invalid_grant”}

Here are the possible reasons for the above error:

  • The date/time of your server is out-of-sync

You can fix this by updating the date/time of your server or by asking your hosting provider to check it and make sure it is on sync. Using NTP sync is recommended.

  • The token has expired

There are multiple reasons for this case. Among them:

– You’ve revoked the authentication.

– The token wasn’t used for a long time and the authentication has expired.

– You’ve authorized the plugin with more than 25 sites using the same Google Analytics account.

Google has a limit of 25 active refresh tokens for a Client/Account pair. Here are some solutions if you need to authorize more than 25 sites:

– If your sites are all on the same WordPress Network (multisite mode), switch on this option: use a single Google Analytics account for the entire network. You’ll be able to use a single refresh token for all your network sites.

– Let your clients have their own Google Accounts. They will authorize the plugin using their own account. If you also need access to their analytics data (maintenance, or other contractual stuff) they can share the Properties/Views using the User Management feature from Google Analytics. (recommended)

– Create multiple API Projects, and authorize 25 sites or less per project. Unfortunately the plugin’s realtime reports won’t work without access to Realtime Reporting API.

– Use multiple Google Accounts of your own to authorize 25 sites or less per account. (not recommended)

Related to Google’s limit of 25 active refresh tokens for a Client/Account pair, if you only need to use the tracking component of the plugin follow these steps:

– Authorize the plugin and select the desired Property/View.

– Open the Backend Settings screen and switch off the following features:

a) enable reports on Posts List and Pages List

b) enable the main Dashboard Widget

– Open the Frontend Settings screen and switch off the following features:

a) enable web page reports on frontend

– Make sure that the frontend widget is disabled under Appearance -> Widgets screen.

By disabling the analytics reports, you will have a fully functional analytics tracking plugin, which won’t be affected by the above limit.

Error fetching OAuth2 access token, message: ‘invalid_grant”

The error is generated when the user doesn’t have a properly configured Google Analytics account. Before authorization please make sure you’ve created at least one account, a property and a view on analytics.google.com.