@Bram @Richard Got google indexing working, Please follow the steps and let me know if this works for you.
Add a new code block after creating your WordPress post/page - As long as we have a URL we want to index, this will work.
Focus on Step 10
Add a new code block and within that add a new variable in inputs named url
which contains the actual URL we want to index.
Open/expand the code editor and click on āAdd npm packageā and enter googleapis
in the package name field.
Your package.json file should look like this
{
"dependencies": {
"googleapis": "133.0.0"
}
}
Now, Replace the entire code you have in your code block with this (index.ts file)
import { google } from 'googleapis'
// the key.json file you downloaded form Google
const key = {
"type": "",
"project_id": "",
"private_key_id": "",
"private_key": "",
"client_email": "",
"client_id": "",
"auth_uri": "",
"token_uri": "",
"auth_provider_x509_cert_url": "",
"client_x509_cert_url": "",
"universe_domain": ""
}
export const code = async (inputs) => {
try {
// create the client
const jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
["https://www.googleapis.com/auth/indexing"],
null
);
const URL_TO_INDEX = inputs.url // This is the url we want to index
const tokens = await jwtClient.authorize()
// make the API call
const response = await fetch("https://indexing.googleapis.com/v3/urlNotifications:publish", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${tokens.access_token}`
},
body: JSON.stringify({ url: URL_TO_INDEX, type: "URL_UPDATED" })
})
if( response.status !== 200 ) {
console.log('error:response', response)
return { success: false }
}
const responseJson = await response.json()
return { success: true, data: responseJson }
} catch (error) {
console.log('catch error', error)
return { success: false, error: error?.message || 'Error occurred' };
}
};
Final step: focus on lines 4-16, This is the data you downloaded from Google key.json
file. Replace this data with your own data.
Test and verify, Hope this works for you guys.
If you get any error, It should be visible in the console tab of code editor.