Skip to content

Commit ee8d6fa

Browse files
committed
Refactor Google API auth to use GIS client
Replaces deprecated gapi.auth2 authentication with Google Identity Services (GIS) for OAuth. Updates script loading, authorization flow, and error handling to improve reliability and future compatibility.
1 parent 82db4eb commit ee8d6fa

File tree

1 file changed

+60
-34
lines changed

1 file changed

+60
-34
lines changed

SearchConsoleMonitor.html

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,75 @@
77
<head>
88
<div class="spacing-start"></div>
99
<title>Indexed Pages Monitor</title>
10-
<script src="https://apis.google.com/js/api.js"></script>
10+
<!-- Load Google APIs client -->
11+
<script src="https://apis.google.com/js/api.js" onload="gapiLoaded()"></script>
12+
<!-- Load Google Identity Services -->
13+
<script src="https://accounts.google.com/gsi/client" async defer onload="gisLoaded()"></script>
1114
</head>
1215
<body>
1316
<h1>Indexed Pages Count</h1>
14-
<button onclick="initClient()">Authorize & Fetch</button>
17+
<button onclick="authorizeAndFetch()">Authorize & Fetch</button>
1518
<div id="output"></div>
1619

1720
<script>
18-
function initClient() {
19-
gapi.load('client:auth2', () => {
20-
gapi.auth2.init({
21-
client_id: '830015351192-38tmspassocrp65dok2mo83d54g9gtav.apps.googleusercontent.com',
22-
scope: 'https://www.googleapis.com/auth/webmasters.readonly'
23-
}).then(() => {
24-
gapi.auth2.getAuthInstance().signIn()
25-
.then(() => {
26-
gapi.client.load('searchconsole', 'v1').then(fetchSitemaps)
27-
.catch(err => showError('API load error', err));
28-
})
29-
.catch(err => showError('Sign-in error', err));
30-
}).catch(err => showError('Init error', err));
31-
});
32-
}
21+
let tokenClient;
22+
let gapiInited = false;
3323

34-
function fetchSitemaps() {
35-
gapi.client.searchconsole.sitemaps.list({
36-
siteUrl: 'https://pwindows.qzz.io/'
37-
}).then(response => {
38-
if (response.result.sitemap && response.result.sitemap.length > 0) {
39-
const data = response.result.sitemap[0].contents[0];
40-
document.getElementById('output').innerText =
41-
`Submitted: ${data.submitted}, Indexed: ${data.indexed}`;
42-
} else {
43-
document.getElementById('output').innerText = 'No sitemap data found.';
24+
// Load GAPI client
25+
function gapiLoaded() {
26+
gapi.load('client', initializeGapiClient);
27+
}
28+
29+
async function initializeGapiClient() {
30+
await gapi.client.init({
31+
discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/searchconsole/v1/rest'],
32+
});
33+
gapiInited = true;
34+
}
35+
36+
// Load GIS client
37+
function gisLoaded() {
38+
tokenClient = google.accounts.oauth2.initTokenClient({
39+
client_id: '830015351192-38tmspassocrp65dok2mo83d54g9gtav.apps.googleusercontent.com',
40+
scope: 'https://www.googleapis.com/auth/webmasters.readonly',
41+
callback: (tokenResponse) => {
42+
if (tokenResponse.access_token) {
43+
fetchSitemaps();
44+
} else {
45+
showError('Token error', tokenResponse);
46+
}
47+
},
48+
});
49+
}
50+
51+
// Trigger authorization
52+
function authorizeAndFetch() {
53+
if (!gapiInited) {
54+
showError('Init error', {message: 'GAPI not initialized yet'});
55+
return;
56+
}
57+
tokenClient.requestAccessToken();
58+
}
59+
60+
// Fetch sitemap data
61+
async function fetchSitemaps() {
62+
try {
63+
const response = await gapi.client.searchconsole.sitemaps.list({
64+
siteUrl: 'https://www.pwindows.qzz.io/',
65+
});
66+
if (response.result.sitemap && response.result.sitemap.length > 0) {
67+
const data = response.result.sitemap[0].contents[0];
68+
document.getElementById('output').innerText =
69+
`Submitted: ${data.submitted}, Indexed: ${data.indexed}`;
70+
} else {
71+
document.getElementById('output').innerText = 'No sitemap data found.';
72+
}
73+
} catch (err) {
74+
showError('Fetch error', err);
75+
}
4476
}
45-
}).catch(err => showError('Fetch error', err));
46-
}
4777

48-
function showError(label, err) {
49-
console.error(label, err);
50-
document.getElementById('output').innerText =
51-
`${label}: ${JSON.stringify(err, null, 2)}`;
52-
}
78+
// Error handler
5379
function showError(label, err) {
5480
console.error(label, err);
5581
document.getElementById('output').innerText =

0 commit comments

Comments
 (0)