Skip to content

Commit

Permalink
fix: Resolves Broken Google Adwords Authentication in Docker containers.
Browse files Browse the repository at this point in the history
closes #179
  • Loading branch information
towfiqi committed Mar 13, 2024
1 parent 75453d8 commit 1d0a788
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions pages/api/adwords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,34 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const getAdwordsRefreshToken = async (req: NextApiRequest, res: NextApiResponse<string>) => {
try {
const code = (req.query.code as string);
// console.log('code :', code);
const https = req.headers.host?.includes('localhost:') ? 'http://' : 'https://';
const redirectURL = `${https}${req.headers.host}/api/adwords`;

if (code) {
try {
const settingsRaw = await readFile(`${process.cwd()}/data/settings.json`, { encoding: 'utf-8' });
const settings: SettingsType = settingsRaw ? JSON.parse(settingsRaw) : {};
const cryptr = new Cryptr(process.env.SECRET as string);
const adwords_client_id = settings.adwords_client_id ? cryptr.decrypt(settings.adwords_client_id) : '';
const adwords_client_secret = settings.adwords_client_secret ? cryptr.decrypt(settings.adwords_client_secret) : '';
const redirectURL = `${process.env.NEXT_PUBLIC_APP_URL}/api/adwords`;
const oAuth2Client = new OAuth2Client(adwords_client_id, adwords_client_secret, redirectURL);
const r = await oAuth2Client.getToken(code);
if (r?.tokens?.refresh_token) {
const adwords_refresh_token = cryptr.encrypt(r.tokens.refresh_token);
await writeFile(`${process.cwd()}/data/settings.json`, JSON.stringify({ ...settings, adwords_refresh_token }), { encoding: 'utf-8' });
return res.status(200).send('Google Ads Intergrated Successfully! You can close this window.');
}
return res.status(200).send('Error Getting the Google Ads Refresh Token. Please Try Again!');
} catch (error) {
console.log('[Error] Getting Google Ads Refresh Token!');
console.log('error :', error);
return res.status(200).send('Error Saving the Google Ads Refresh Token. Please Try Again!');
return res.status(400).send('Error Getting the Google Ads Refresh Token. Please Try Again!');
} catch (error:any) {
const errorMsg = error?.response?.data?.error;
console.log('[Error] Getting Google Ads Refresh Token! Reason: ', errorMsg);
return res.status(400).send(`Error Saving the Google Ads Refresh Token ${errorMsg ? `. Details: ${errorMsg}` : ''}. Please Try Again!`);
}
} else {
return res.status(200).send('No Code Provided By Google. Please Try Again!');
return res.status(400).send('No Code Provided By Google. Please Try Again!');
}
} catch (error) {
console.log('[ERROR] CRON Refreshing Keywords: ', error);
console.log('[ERROR] Getting Google Ads Refresh Token: ', error);
return res.status(400).send('Error Getting Google Ads Refresh Token. Please Try Again!');
}
};
Expand Down

0 comments on commit 1d0a788

Please sign in to comment.