towfiqi / serpbear

Search Engine Position Rank Tracking App
https://docs.serpbear.com/
MIT License
1.38k stars 136 forks source link

Error Saving the Google Ads Refresh Token #179

Closed graphiostudio closed 6 months ago

graphiostudio commented 7 months ago

When trying to authenticate with Google Ads account, getting this: "Error Saving the Google Ads Refresh Token. Please Try Again!". Container volume is writable. Logs are as below:

[0] [Error] Getting Google Ads Refresh Token!
[0] error : GaxiosError: invalid_grant
[0]     at Gaxios._request (/app/node_modules/gaxios/build/src/gaxios.js:142:23)
[0]     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
[0]     at async OAuth2Client.getTokenAsync (/app/node_modules/google-auth-library/build/src/auth/oauth2client.js:137:21)
[0]     at async getAdwordsRefreshToken (/app/.next/server/pages/api/adwords.js:143:27)
[0]     at async Object.apiResolver (/app/node_modules/next/dist/server/api-utils/node.js:366:9)
[0]     at async NextNodeServer.runApi (/app/node_modules/next/dist/server/next-server.js:481:9)
[0]     at async Object.fn (/app/node_modules/next/dist/server/next-server.js:741:37)
[0]     at async Router.execute (/app/node_modules/next/dist/server/router.js:252:36)
[0]     at async NextNodeServer.run (/app/node_modules/next/dist/server/base-server.js:365:29)
[0]     at async NextNodeServer.handleRequest (/app/node_modules/next/dist/server/base-server.js:303:20) {
[0]   config: {
[0]     method: 'POST',
[0]     url: 'https://oauth2.googleapis.com/token',
[0]     data: '<<REDACTED> - See `errorRedactor` option in `gaxios` for configuration>.',
[0]     headers: {
[0]       'Content-Type': 'application/x-www-form-urlencoded',
[0]       'User-Agent': 'google-api-nodejs-client/9.6.3',
[0]       'x-goog-api-client': 'gl-node/20.11.1'
[0]     },
[0]     paramsSerializer: [Function: paramsSerializer],
[0]     body: '<<REDACTED> - See `errorRedactor` option in `gaxios` for configuration>.',
[0]     validateStatus: [Function: validateStatus],
[0]     responseType: 'unknown',
[0]     errorRedactor: [Function: defaultErrorRedactor]
[0]   },
[0]   response: {
[0]     config: {
[0]       method: 'POST',
[0]       url: 'https://oauth2.googleapis.com/token',
[0]       data: '<<REDACTED> - See `errorRedactor` option in `gaxios` for configuration>.',
[0]       headers: [Object],
[0]       paramsSerializer: [Function: paramsSerializer],
[0]       body: '<<REDACTED> - See `errorRedactor` option in `gaxios` for configuration>.',
[0]       validateStatus: [Function: validateStatus],
[0]       responseType: 'unknown',
[0]       errorRedactor: [Function: defaultErrorRedactor]
[0]     },
[0]     data: { error: 'invalid_grant', error_description: 'Bad Request' },
[0]     headers: {
[0]       'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000',
[0]       'cache-control': 'no-cache, no-store, max-age=0, must-revalidate',
[0]       'content-encoding': 'gzip',
[0]       'content-type': 'application/json; charset=utf-8',
[0]       date: 'Wed, 06 Mar 2024 10:30:44 GMT',
[0]       expires: 'Mon, 01 Jan 1990 00:00:00 GMT',
[0]       pragma: 'no-cache',
[0]       server: 'scaffolding on HTTPServer2',
[0]       'transfer-encoding': 'chunked',
[0]       vary: 'Origin, X-Origin, Referer',
[0]       'x-content-type-options': 'nosniff',
[0]       'x-frame-options': 'SAMEORIGIN',
[0]       'x-xss-protection': '0'
[0]     },
[0]     status: 400,
[0]     statusText: 'Bad Request',
[0]     request: { responseURL: 'https://oauth2.googleapis.com/token' }
[0]   },
[0]   error: undefined,
[0]   status: 400,
[0]   [Symbol(gaxios-gaxios-error)]: '6.3.0'
[0] }
towfiqi commented 6 months ago

@graphiostudio Just ran the app on a live server with an actual domain, and everything worked fine. However, I did not use Docker. Are you running the app with Docker?

towfiqi commented 6 months ago

Turns out it was a bug specific to how Nextjs handles the NEXT_PUBLIC_APP_URL variable. #17641 When the app is built, it actually uses the value that was set on the built machine which means it's almost hardcoded. This only happens with docker installation. In any case, had to workaround this by grabbing the domain name from the request header and building the redirect URL using that.

So, those who are having this issue, update to version 2.0.2, and the issue should be fixed.

m4nug commented 6 months ago

Great news. Thanks for your hard work and sorry I couldn't find more time to help you.

towfiqi commented 6 months ago

@m4nug No worries!

graphiostudio commented 6 months ago

Worked on the first try now :) awesome, thank you.

saschafoerster commented 6 months ago

Worked like a charm now. :)

ingeniumdesign commented 6 months ago

hi, i have a domain/portforward from duckdns to my fritzbox. i edit and add the NEXT_PUBLIC_APP_URL for the new duckdns url with port.

Portainer: NEXT_PUBLIC_APP_URL http://XXXXX.duckdns.org:13000 => Port:3000

Edit and add the API URL: http://XXXXX.duckdns.org:13000/api/adwords

The tool works fine with the duckdns url...

The Google Search Console Integrate works fine. The Scraping work fine.

but the Integrate Google Ads dont work, and i have the Error: "_Error Saving the Google Ads Refresh Token . Details: redirect_urimismatch. Please Try Again!"

SerpBear v2.0.2