I'm trying to use the google-api-javascript-client with the Adsense Management API to get reports of an authorized account. The login process with OAuth2 is working fine and I can fetch the Adsense Account data associated to the logged-in Google Account. (see generateReportExmaple -> accountList) However when I try to get reports or work with any other resource (site, reports...) I get the following error:
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)"}
I found a few examples in the github repo of the api-js-client but they are quite oudated. I'm also confused why the reports object is attached to the accounts object of the gapi client. In all there examples the reports are in the adsense object. In my case all are attached in adsense.accounts.
This is my code:
<!--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>