Google Indexing API with Python

966 views Asked by At

Here is my Code snippet to request google indexing using python.

from oauth2client.service_account import ServiceAccountCredentials
import httplib2

SCOPES = [ "https://www.googleapis.com/auth/indexing" ]
ENDPOINT = "https://indexing.googleapis.com/v3/urlNotifications:publish"

# service_account_file.json is the private key that you created for your service account.
JSON_KEY_FILE = "myJSON.json"

credentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEY_FILE, scopes=SCOPES)

http = credentials.authorize(httplib2.Http())


# Define contents here as a JSON string
content = """
{
"url": "https://locksro.weebly.com/",
"type": "URL_UPDATED"
}"""
    #send the request
response, content = http.request(ENDPOINT, method="POST", body=content)
print(response.status)

To ensure that the Link Indexed, I check it by searching {site:locksro.weebly.com} in google, but no result. I cannot figure out the problem as the response.status is 200

2

There are 2 answers

0
JTX On

If anyone is reading this confused about long content string and want to use variables

You can also send like below

    SCOPES = ["https://www.googleapis.com/auth/indexing"]
    ENDPOINT = "https://indexing.googleapis.com/v3/urlNotifications:publish"
    JSON_KEY_FILE = "firebase.json"
    try:
        credentials = ServiceAccountCredentials.from_json_keyfile_name(
            JSON_KEY_FILE, scopes=SCOPES
        )
        http = credentials.authorize(httplib2.Http())
        body = {"url": url, "type": "URL_UPDATED"}
        response, content = http.request(
            ENDPOINT, method="POST", body=json.dumps(body)
        )
    except Exception as e:
        pass
1
HUSSINE VTM On

it worked me

import httplib2
 
SCOPES = [ "https://www.googleapis.com/auth/indexing" ]
ENDPOINT = "https://indexing.googleapis.com/v3/urlNotifications:publish"
 
# service_account_file.json is the private key that you created for your service account.
JSON_KEY_FILE = "myJSON.json"
 
credentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEY_FILE, scopes=SCOPES)
 
http = credentials.authorize(httplib2.Http())
 
# Define contents here as a JSON string.
# This example shows a simple update request.
# Other types of requests are described in the next step.
 
content = """{
  "url": "https://mysmartpros.com/tutor-tuition-online-jobs/economics",
  "type": "URL_UPDATED"
}"""
 
response, content = http.request(ENDPOINT, method="POST", body=content)
 
print(response) 
print(content)```

{'content-type': 'application/json; charset=UTF-8', 'vary': 'Origin, X-Origin, Referer', 'date': 'Mon, 25 Jan 2021 23:48:59 GMT', 'server': 'ESF', 'cache-control': 'private', 'x-xss-protection': '0', 'x-frame-options': 'SAMEORIGIN', 'x-content-type-options': 'nosniff', 'transfer-encoding': 'chunked', 'status': '200', 'content-length': '299', '-content-encoding': 'gzip'}


b'{\n  "urlNotificationMetadata": {\n    "url": "https://mysmartpros.com/tutor-tuition-online-jobs/economics",\n    "latestUpdate": {\n      "url": "https://mysmartpros.com/tutor-tuition-online-jobs/economics",\n      "type": "URL_UPDATED",\n      "notifyTime": "2021-01-25T23:48:59.783031464Z"\n    }\n  }\n}\n'