Search code examples
node.jsgoogle-api-nodejs-clientadsense-api

Node.js cannot access Google AdSense Management API. 'user does not have adsense account'


I'm trying to generate AdSense reports on web app hosted on node.js. Since it's a server-to-server communication i need to use service account. I followed this guide, but i always getting error:

{
 domain: "global"
 message: "User does not have an AdSense account."
 reason: "noAdSenseAccount"
}

also tried this JSON Web Tokens Guide but also getting error above.

What i got:

  • The Adsense Management API is enabled.
  • Service account credentials are created
  • Added service account email address to google AdSense User Management
  • Tested in the Google API Explorer and it works just fine

screenshot

NOTE: its status stuck on "pending".

Here is the code i'm trying to connect and retrieve data with:

const googleAuth = require('google-auth-library');
const GOOGLE_KEYS = require(path.join(__dirname, '/credentials/jwt.keys.json'));

this.jwtClient = new googleAuth.JWT(
  GOOGLE_KEYS.client_email,
  null,
  GOOGLE_KEYS.private_key,
  ['https://www.googleapis.com/auth/adsense'],
);
this.jwtClient.authorize(async (err: any, tokens: any) => {
  if (err) throw new Error(err);
  this.jwtClient.setCredentials(tokens);
  const url = `https://www.googleapis.com/adsense/v1.4/accounts`;
  const listData = await this.jwtClient.request({url})
    .catch((err: any) => {
      console.log(err, 'error');
      res.status(500).json(err)
     });
  res.status(200).json(data);
});

My questions is:
1. Why is it stuck on "pending" status?
2. Is there a way to acceess API using service account credentials?
3. If yes which way i can achieve it?


Solution

  • The AdSense API doesn't support Service Accounts[1], so you'll need to make OAuth credentials using the Web App flow[2].