Opteo / google-ads-api

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

Authentication Error #479

Closed mmodoucham closed 2 weeks ago

mmodoucham commented 1 year ago

Hello, I am getting the follwoing error trying to fetch list of customers

code: 16, details: 'Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.', metadata: Metadata { internalRepr: Map(3) { 'google.ads.googleads.v14.errors.googleadsfailure-bin' => [Array], 'grpc-status-details-bin' => [Array], 'request-id' => [Array] }, options: {} }, note: 'Exception occurred in retry method that was not classified as transient'

I generated a refresh token using the helper here: https://refresh-token-helper.opteo.com/

my code looks like this: ` const getCustomers = async (req, res) => { const client = new GoogleAdsApi({ client_id: process.env.CLIENT_ID, client_secret: process.env.CLIENT_SECRET, developer_token: process.env.DEVELOPER_TOKEN, });

const refreshToken = process.env.REFRESH_TOKEN; try { const customers = await client.listAccessibleCustomers(refreshToken); res.send({ customers, }); } catch (error) { res.send({ error, }); } }; `

donqq commented 1 month ago

Don't use any third party tools. Use this script to generate the refresh token


const { OAuth2Client } = require('google-auth-library');
const readline = require('readline');
require('dotenv').config({ path: './Google/.env' });

const clientId = process.env.CLIENT_ID;
const clientSecret = process.env.CLIENT_SECRET;
const redirectUri = 'http://localhost:8080';

const oauth2Client = new OAuth2Client(
  clientId,
  clientSecret,
  redirectUri
);

const scopes = ['https://www.googleapis.com/auth/adwords'];

const authUrl = oauth2Client.generateAuthUrl({
  access_type: 'offline',
  scope: scopes,
  prompt: 'consent',
});

console.log('Authorize this app by visiting this URL:', authUrl);

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.question('Enter the code from that page here: ', async (code) => {
  rl.close();
  try {
    const { tokens } = await oauth2Client.getToken(code);
    console.log('Your refresh token is:', tokens.refresh_token);
    // Save the refresh token securely
  } catch (err) {
    console.error('Error retrieving refresh token:', err);
  }
});