Opteo / google-ads-api

Google Ads API client library for Node.js
https://opteo.com
MIT License
270 stars 90 forks source link

Refresh token expiring? Error: 16 UNAUTHENTICATED: Failed to retrieve auth metadata with error: invalid_grant #413

Closed sgpascoe closed 1 year ago

sgpascoe commented 2 years ago
  code: 16,
  details: 'Failed to retrieve auth metadata with error: invalid_grant',
  metadata: Metadata { internalRepr: Map(0) {}, options: {} },
  note: 'Exception occurred in retry method that was not classified as transient'

It seems like this keeps happening every week or so, and I need to go through the process of getting a new refresh token every time from here, which then fixes the issue.

mkosik commented 2 years ago

+1 can some of the lib authors please elaborate on this one?

sgpascoe commented 2 years ago

I'm tempted to implement a puppeteer function to fetch it's own refresh token if it's not working 🤣

wcoots commented 2 years ago

Hi @sgpascoe @mkosik, apologies for the delayed response, we are very busy at the moment at Opteo. This library is only a wrapper around the Google Ads API, we are not involved in refresh tokens. I suggest asking your question instead at the Google Ads API forum.

sgpascoe commented 2 years ago

@wcoots This library is only a wrapper around the Google Ads API, we are not involved in refresh tokens. I suggest asking your question instead at the Google Ads API forum.

They have replied:

Hi Sam,

Thank you for posting your concern. Allow me to provide support.

I can see that you encountered an invalid_grant error, it occurs when the refresh token has been expired or revoked. The common cause of this error is when a Google Cloud Platform project with an OAuth consent screen configured for an external user type and a publishing status of "Testing" is issued a refresh token expiring in 7 days. Your Google project's publishing status is "Testing", so the refresh token expires every 7 days and receives an “invalid_grant” error. Go to the Google API Console and navigate to the OAuth consent screen. Then change the publishing status to “In production” following these instructions to avoid the refresh token expiring in 7 days.

Is this not related to the token given here: https://refresh-token-helper.opteo.com/ ?

avermeil commented 1 year ago

Hi @sgpascoe -- The refresh token helper tool is meant as a quick way to get started, not really as a permanent solution :)

That said, based on Google Support's response, it sounds like you'll get longer-lived tokens if you change your Google Project's publishing status (likely the one associated with the client_id you're using in the helper tool).

Consider checking the helper tool's source code -- it shows just how simple it is to get new tokens. Certainly much faster than setting up a puppeteer scraper!