0

我正在尝试在 node.js 上托管的网络应用程序上生成 AdSense 报告。由于它是服务器到服务器的通信,我需要使用服务帐户。我按照指南进行操作,但总是出现错误:

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

也试过这个 JSON Web Tokens Guide但上面也出现错误。

我得到了什么:

  • Adsense 管理 API 已启用。
  • 已创建服务帐户凭据
  • 将服务帐户电子邮件地址添加到 google AdSense 用户管理
  • 在 Google API Explorer 中测试,它工作得很好

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.如果是,我可以通过哪种方式实现?

4

1 回答 1

0

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

于 2019-04-24T15:13:16.367 回答