Node.js无法访问Google AdSense Management API。 “用户没有adsense帐户”

时间:2019-04-24 13:24:43

标签: node.js google-api-nodejs-client adsense-api

我正在尝试在node.js托管的网络应用上生成AdSense报告。 由于这是服务器到服务器的通信,因此我需要使用服务帐户。 我遵循了this指南,但是我总是遇到错误:

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

还尝试了this JSON Web Tokens Guide,但上面也出现了错误。

我得到了什么:

  • 已启用Adsense管理API。
  • 服务帐户凭据已创建
  • 向Google AdSense用户管理中添加了服务帐户电子邮件地址
  • 在Google API资源管理器中进行了测试,效果很好

screenshot

注意:其状态停留在“待处理”状态。

这是我尝试使用以下代码连接和检索数据的代码:

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);
});

我的问题是:
1.为什么它停留在“待定”状态?
2.是否可以使用服务帐户凭据来访问API?
3.如果可以,我可以通过哪种方式实现它?

1 个答案:

答案 0 :(得分:0)

AdSense API不支持服务帐户[1],因此您需要使用Web App流[2]制作OAuth凭据。