【问题标题】:Use Adsense Management V2 API with Google API JavaScript client Wa`parent Error将 Adsense Management V2 API 与 Google API JavaScript 客户端 Wa`parent 错误一起使用
【发布时间】:2021-08-29 12:29:26
【问题描述】:

我正在尝试将 google-api-javascript-client 与 Adsense 管理 API 一起使用以获取授权帐户的 reports。使用 OAuth2 的登录过程运行良好,我可以获取与登录的 Google 帐户关联的 Adsense 帐户数据。 (请参阅generateReportExmaple -> accountList)但是,当我尝试获取报告或使用任何其他资源(站点、报告...)时,我收到以下错误:

Uncaught (in promise) pB 
{
  message: "Wa`parent", 
  SL: true, 
  stack: "gapi.client.Error: Wa`parent
    at new pB (https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.de.soliK2B9LKA.O/m=auth2,client/rt=j/sv=1/d=1/ed=1/am=AQ/rs=AGLTcCP_VSmeyDlYE1vxFyfmddhL6RM9dw/cb=gapi.loaded_0:1003:190)
    at Object.<anonymous> (https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.de.soliK2B9LKA.O/m=auth2,client/rt=j/sv=1/d=1/ed=1/am=AQ/rs=AGLTcCP_VSmeyDlYE1vxFyfmddhL6RM9dw/cb=gapi.loaded_0:1067:432)
    at Object._.em (https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.de.soliK2B9LKA.O/m=auth2,client/rt=j/sv=1/d=1/ed=1/am=AQ/rs=AGLTcCP_VSmeyDlYE1vxFyfmddhL6RM9dw/cb=gapi.loaded_0:454:399)
    at https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.de.soliK2B9LKA.O/m=auth2,client/rt=j/sv=1/d=1/ed=1/am=AQ/rs=AGLTcCP_VSmeyDlYE1vxFyfmddhL6RM9dw/cb=gapi.loaded_0:1067:147
    at Object._.dm (https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.de.soliK2B9LKA.O/m=auth2,client/rt=j/sv=1/d=1/ed=1/am=AQ/rs=AGLTcCP_VSmeyDlYE1vxFyfmddhL6RM9dw/cb=gapi.loaded_0:454:201)
    at CC (https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.de.soliK2B9LKA.O/m=auth2,client/rt=j/sv=1/d=1/ed=1/am=AQ/rs=AGLTcCP_VSmeyDlYE1vxFyfmddhL6RM9dw/cb=gapi.loaded_0:1067:128)
    at Object.AC (https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.de.soliK2B9LKA.O/m=auth2,client/rt=j/sv=1/d=1/ed=1/am=AQ/rs=AGLTcCP_VSmeyDlYE1vxFyfmddhL6RM9dw/cb=gapi.loaded_0:1066:48)
    at Object.list (https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.de.soliK2B9LKA.O/m=auth2,client/rt=j/sv=1/d=1/ed=1/am=AQ/rs=AGLTcCP_VSmeyDlYE1vxFyfmddhL6RM9dw/cb=gapi.loaded_0:176:135)
    at generateReportExmaple (http://localhost:5500/stack.html:64:91)"}

我找到了 api-js-client 的 few examples in the github repo,但它们已经过时了。我也很困惑为什么 reports 对象附加到 gapi 客户端的 accounts 对象。在所有示例中,reports 都在 adsense 对象中。就我而言,所有内容都附在adsense.accounts中。

这是我的代码:

<!--Add buttons to initiate auth sequence and sign out-->
<button id="authorize-button" style="display: none;">Authorize</button>
<button id="signout-button" style="display: none;">Sign Out</button>

<script>
  const apiKey = 'API_KEY';
  const discoveryDocs = ["https://adsense.googleapis.com/$discovery/rest?version=v2"];
  const clientId = 'CLIENT_ID_KEY.apps.googleusercontent.com';
  const scopes = "profile https://www.googleapis.com/auth/adsense https://www.googleapis.com/auth/adsense.readonly";
  
  const authorizeButton = document.getElementById('authorize-button');
  const signoutButton = document.getElementById('signout-button');
  
  function handleClientLoad() {
      gapi.load('client:auth2', initClient);
  }
  
  async function initClient() {
      await gapi.client.init({
          apiKey: apiKey,
          discoveryDocs: discoveryDocs,
          clientId: clientId,
          scope: scopes
      })
      gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
      updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
      authorizeButton.onclick = handleAuthClick;
      signoutButton.onclick = handleSignoutClick;
  }
  
  // Here I fetch the data
  async function generateReportExmaple() {
      const accountsList = await gapi.client.adsense.accounts.list() // Result is fine
  
      const savedReports = await gapi.client.adsense.reports.saved.list() // Cannot read property 'saved' of undefined
      
      const savedReportsStrangeError = await gapi.client.adsense.accounts.reports.saved.list() // cb=gapi.loaded_0:1003 Uncaught (in promise) pB {message: "Wa`parent"....
  }
  
  function updateSigninStatus(isSignedIn) {
      if (isSignedIn) {
          authorizeButton.style.display = 'none';
          signoutButton.style.display = 'block';
          generateReportExmaple()
      } else {
          authorizeButton.style.display = 'block';
          signoutButton.style.display = 'none';
      }
  }
  
  function handleAuthClick(event) {
      gapi.auth2.getAuthInstance().signIn();
  }
  
  function handleSignoutClick(event) {
      gapi.auth2.getAuthInstance().signOut();
  }
</script>
<script async defer src="https://apis.google.com/js/api.js" onload="this.onload=function(){};handleClientLoad()"
  onreadystatechange="if (this.readyState === 'complete') this.onload()"></script>

【问题讨论】:

    标签: javascript google-api google-api-client google-api-js-client adsense-api


    【解决方案1】:

    我不确定您为什么会收到如此奇怪的错误消息,但您看到的两个问题可以解释为:

    1. reports.saved.list 方法仅存在于 adsense v1.4 中,而您使用的是 v2,因此 adsense.reports Javascript 对象不存在。
    2. accounts.reports.saved.list 方法has a required parent parameter 指定您正在访问已保存报告的帐户。 (例如,这与调用accounts.list 得到的结果中的accounts[].name 之一相同。)

    【讨论】:

    猜你喜欢
    • 2020-06-09
    • 2012-10-12
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 2017-12-15
    • 2013-01-19
    • 2016-10-12
    相关资源
    最近更新 更多